+ 2

Someone helps, How to Run Output this guys to see my result code, I try to output this but it doesn't work for what i thought

package com.java.tutorialStreamAPI; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class CarCollection { private static List<Car> cars = List.of( new Car("RollsRoyce", 2000, "Italy"), new Car("Lexus", 1800, "USA"), new Car("BMX", 1900.07, "Germany") ); public static void main(String[] args) { Stream<Car> stream = cars.stream(); stream .map(x -> x.name.toLowerCase()) .filter(x -> x.startsWith("r")) .collect(Collectors.toList()); //System.out.println(stream); XXXXXX how??? } }

17th Jun 2022, 3:23 PM
Samnang Choeurn
Samnang Choeurn - avatar
7 Answers
+ 1
Where is your Car class implementation? you are not saving stream result. Save to list. or instead of collect as list, use .forEach(System.out::println); or put last Statement in System.out.println(< here >);
17th Jun 2022, 3:43 PM
Jayakrishna 🇮🇳
+ 2
because the stream maps Car to name the resulting List must be parameterized as String List<String> rCarNames = stream .map( ... ) .filter( .... ) .collect( ... ); System.out.println( rCarNames);
17th Jun 2022, 5:33 PM
zemiak
+ 1
You don't have System.out.println anywhere in your code.
17th Jun 2022, 3:24 PM
Justice
Justice - avatar
+ 1
🚨 Don't DM me. State your problem here. If you are not getting the output you want then you need to describe the output you want or the task your trying to tackle. It is also better to link your code as a codebit because it is hard to debug a wall of text.
17th Jun 2022, 3:30 PM
Justice
Justice - avatar
+ 1
The collect method will return a list of cars (List<Car>). Create a new variable and assign the return value to it. List<Car> filteredCars = stream.map(...).filter(...).collect(...); You can then loop through the list and print whatever you want.
17th Jun 2022, 3:45 PM
Mustafa A
Mustafa A - avatar
0
Yes , it had succeed when I replaced collect to forEach ,Thank you all guys!
18th Jun 2022, 10:28 AM
Samnang Choeurn
Samnang Choeurn - avatar
0
mo
18th Jun 2022, 10:51 PM
راصطه راصطه
راصطه راصطه - avatar