How to collect Stream> into Map> using java 8?

前端 未结 2 1584
星月不相逢
星月不相逢 2020-12-19 21:48

I have a stream of Map that I want to collect into a single Map>. Does anybody have a suggesti

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 22:53

    Say i had:

    Stream> mapStream
    

    Then the answer is:

    mapStream.map(Map::entrySet)
             .flatMap(Collection::stream)
             .collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
    

提交回复
热议问题