Answers for "java hashmap group by value then key"

0

group all keys with same values in a hashmap java

Map<String, ArrayList<Integer>> reverseMap = new HashMap<>();

for (Map.Entry<Integer,String> entry : map.entrySet()) {
    if (!reverseMap.containsKey(entry.getValue())) {
        reverseMap.put(entry.getValue(), new ArrayList<>());
    }
    ArrayList<Integer> keys = reverseMap.get(entry.getValue());
    keys.add(entry.getKey());
    reverseMap.put(entry.getValue(), keys);
}
Posted by: Guest on August-24-2020

Code answers related to "java hashmap group by value then key"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language