To invert a map, so that its distinct values become keys, and its keys are added to a collection under the corresponding value, use groupingBy()
on the map entries. It's important that the values from the original map implement equals()
and hashCode()
correctly to be used as a key in the new hash table .
static <K, V> Map<V, Set<K>> invert(Map<? extends K, ? extends V> original) {
return original.entrySet().stream()
.collect(Collectors.groupingBy(
Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey, Collectors.toSet())
));
}
If you want to groups to be sorted, you can create a specialized "downstream" collector:
static <K, V> Map<V, SortedSet<K>> invert(
Map<? extends K, ? extends V> original,
Comparator<? super K> order) {
Collector<K, ?, SortedSet<K>> toSortedSet =
Collectors.toCollection(() -> new TreeSet<>(order));
return original.entrySet().stream()
.collect(Collectors.groupingBy(
Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey, toSortedSet)
));
}