Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to collect multiple lists to one list with java-streams? [duplicate]

Tags:

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?

like image 672
membersound Avatar asked Jun 06 '16 07:06

membersound


1 Answers

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()); 
like image 73
Eran Avatar answered Sep 30 '22 13:09

Eran