public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("abc", 2);
map.put("def", 1);
map.put("hij", 4);
map.put("klm", 6);
map.put("nop", 2);
map.put("qrs", 2);
map.put("tuv", 6);
map.put("wxy", 8);
map.put("zab", 1);
map.put("cde", 5);
map.put("fgh", 4);
map.put("ijk", 3);
HashMap<Integer, String> duplicatMap = new HashMap<>();
Set<Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Entry<String, Integer>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
String key = entry.getKey();
Integer value = entry.getValue();
if(duplicatMap.containsKey(value)) {
duplicatMap.put(value, duplicatMap.get(value)+", "+key);
} else {
duplicatMap.put(value, key);
}
}
System.out.println(duplicatMap);
}
outPut: - {1=def, zab, 2=abc, qrs, nop, 3=ijk, 4=fgh, hij, 5=cde, 6=tuv, klm, 8=wxy}
if you want to modify then use again EntrySet.