Is there a more elegant/built-in way to reverse the keys and values of a Hashmap?
I currently have the following.
private Map
You might consider using one of Guava's Multimap implementations. For example:
private Multimap reverseMap(Map permissions) {
Multimap multimap = ArrayListMultimap.create();
for (Map.Entry entry : permissions.entrySet()) {
multimap.put(entry.getValue(), entry.getKey());
}
return multimap;
}
Or more generally:
private static Multimap reverseMap(Map source) {
Multimap multimap = ArrayListMultimap.create();
for (Map.Entry entry : source.entrySet()) {
multimap.put(entry.getValue(), entry.getKey());
}
return multimap;
}