I have a Stream< Map< K, V > >
and I\'m trying to merge those maps together, but preserve duplicate values in a list, so the final type would be
Use groupingBy: see the javadoc, but in your case it should be something like that:
a.flatMap(map -> map.entrySet().stream())
.collect(
Collectors.groupingBy(
Map.Entry::getKey,
HashMap::new,
Collectors.mapping(Map.Entry::getValue, toList())
)
);
Or:
a.map(Map::entrySet).flatMap(Set::stream)
.collect(Collectors.groupingBy(
Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, toList())
)
);