How can I collect multiple List
values into one list, using java-streams
?
List<MyListService> services; services.stream().XXX.collect(Collectors.toList()); interface MyListService { List<MyObject> getObjects(); }
As I have full control over the interface: or should I change the method to return an Array instead of a List?
You can collect the Lists contained in the MyListService
instances with flatMap
:
List<MyObject> list = services.stream() .flatMap(s -> s.getObjects().stream()) .collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With