Answers for "get all similar values in hashmap"

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 "get all similar values in hashmap"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language