Hi folks,
I noticed that Collections doesn't have the equivalent of Guava's
inverse() for ImmutableMultimaps or Eclipse Collections flip() for its
multimaps. Would there be interest in looking at a PR for such
functionality?
Here is an example:
Map<String, Set<String>> citiesLived = new HashSetValuedHashMap<>(4);
citiesLived.put("Alice", "N.Y.");
citiesLived.put("Alice", "L.A.");
citiesLived.put("Alice", "Chicago");
citiesLived.put("Bob", "N.Y.");
citiesLived.put("Cara", "L.A.");
citiesLived.put("Cara", "Chicago");
Here is the proposed syntax:
Map<String, Set<String>> inverted = citiesLived.inverse()
The current way to get this would be iterating over entries and
creating the inverse multimap manually or use streams like below:
Map<String, Set<String>> inverted = citiesLived.entries().stream()
.collect(Collectors.groupingBy(
Map.Entry::getValue,
// group by city
Collectors.mapping(Map.Entry::getKey, // collect names
Collectors.toSet())
));
System.out.println("Original: " + citiesLived);
System.out.println("Inverted: " + inverted);
Output would be:
Original: {Cara=[L.A., Chicago], Bob=[N.Y.], Alice=[L.A., N.Y., Chicago]}
Inverted: {N.Y.=[Bob, Alice], Chicago=[Cara, Alice], L.A.=[Cara, Alice]}
Thoughts?
Cheers, Paul.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]