我有一组
谢谢!
Map<String,Double>
数据流,我想将它们汇总成一个Map<String,List<Double>>
。请问有什么建议吗?谢谢!
Map<String,Double>
数据流,我想将它们汇总成一个Map<String,List<Double>>
。请问有什么建议吗?首先,您需要将您的映射流平展成一个映射条目流。然后,使用 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())));
假设我有以下内容:
Stream<Map<String, Double>> mapStream
mapStream.map(Map::entrySet)
.flatMap(Collection::stream)
.collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));