+ 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??? } }
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 >);
+ 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);
+ 1
You don't have System.out.println anywhere in your code.
+ 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.
+ 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.
0
Yes , it had succeed when I replaced collect to forEach ,Thank you all guys!
0
mo