Java 8: merge lists with stream API

前端 未结 3 405
死守一世寂寞
死守一世寂寞 2020-12-07 22:17

I have the following situation

Map map; 

public class ListContainer{
  List lst;
}

I have to merge

相关标签:
3条回答
  • 2020-12-07 22:52

    Alternative: Stream.concat()

    Stream.concat(map.values().stream(), listContainer.lst.stream())
                                 .collect(Collectors.toList()
    
    0 讨论(0)
  • 2020-12-07 22:52

    In Java 8 we can use stream List1.stream().collect(Collectors.toList()).addAll(List2); Another option List1.addAll(List2)

    0 讨论(0)
  • 2020-12-07 23:00

    I think flatMap() is what you're looking for.

    For example:

     List<AClass> allTheObjects = map.values()
             .stream()
             .flatMap(listContainer -> listContainer.lst.stream())
             .collect(Collectors.toList());
    
    0 讨论(0)
提交回复
热议问题