Answers for "key and value have same string"

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 "key and value have same string"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language