How to use Java stream to collect multiple lists into one list?
•
Java
There is already an answer to this question: > java8 lambda: concat LIST1
List<MyListService> services; services.stream().XXX.collect(Collectors.toList()); interface MyListService { List<MyObject> getObjects(); }
Because I completely control the interface: or should I change the method to return an array instead of a list?
Solution
You can use flatmap to collect the lists contained in the mylistservice instance:
List<MyObject> list = services.stream() .flatMap(s -> s.getObjects().stream()) .collect(Collectors.toList());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码