group all keys with same values in a hashmap java code example
Example: group all keys with same values in a hashmap java
Map> reverseMap = new HashMap<>();
for (Map.Entry entry : map.entrySet()) {
if (!reverseMap.containsKey(entry.getValue())) {
reverseMap.put(entry.getValue(), new ArrayList<>());
}
ArrayList keys = reverseMap.get(entry.getValue());
keys.add(entry.getKey());
reverseMap.put(entry.getValue(), keys);
}