I have a stream of Map<String,Double> that I want to collect into a single Map<String,List<Double>>. Does anybody have a suggestion on how to do this?
Map<String,Double>
Map<String,List<Double>>
Thanks!
First you need to flatten your stream of maps into a stream of map entries. Then, use Collectors.groupingBy along with Collectors.mapping:
Collectors.groupingBy
Collectors.mapping
Map<String,List<Double>> result = streamOfMaps .flatMap(map -> map.entrySet().stream()) .collect(Collectors.groupingBy( Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
2.1m questions
2.1m answers
60 comments
57.0k users