Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

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);
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #group #keys #values #hashmap #java
ADD COMMENT
Topic
Name
8+5 =