group all keys with same values in a hashmap java

Solutions on MaxInterview for group all keys with same values in a hashmap java by the best coders in the world

showing results for - "group all keys with same values in a hashmap java"
Sophie
14 Feb 2018
1Map<String, ArrayList<Integer>> reverseMap = new HashMap<>();
2
3for (Map.Entry<Integer,String> entry : map.entrySet()) {
4    if (!reverseMap.containsKey(entry.getValue())) {
5        reverseMap.put(entry.getValue(), new ArrayList<>());
6    }
7    ArrayList<Integer> keys = reverseMap.get(entry.getValue());
8    keys.add(entry.getKey());
9    reverseMap.put(entry.getValue(), keys);
10}