I can't get my head around this problem.
Problem
I want to writhe this with Java streams:
List<Manufacturer> manufacturers = ... // List of Manufacturer-Objects
List<List<Car>> cars = new ArrayList<>();
for (Manufacturer man : manufacturers) {
cars.add(man.getCars()); // Let's just say getCars() returns a List of cars.
}
I think with Java Streams it should look like the following:
List<List<Car>> cars = manufacturers.stream().
.flatMap(man -> man.getCars().stream())
.collect(Collectors.toList());
Errors
But my IDE says the following no instance(s) of type variable(s) R exist so that List<Event> conforms to Stream<? extends R>
And the java compiler:
java: incompatible types: cannot infer type-variable(s) R
(argument mismatch; bad return type in lambda expression
java.util.List<elements.Car> cannot be converted to java.util.stream.Stream<? extends R>)
Any solutions for this? Thanks!
> cars = manufacturers.stream() .map(man -> man.getCars()) .collect(Collectors.toList());`
– Hadi J Mar 30 '20 at 14:24> result = manufacturers.stream() .map(Manufacturer::getCars) .collect(Collectors.toList());`
– Ravindra Ranwala Mar 30 '20 at 14:34