Seems I can't understand my code :), with this map, iterator and for-each loop
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
HashMap<String, String> map = new HashMap<>();
map.put("Marlin","Johnson");
map.put("Martin","Johnson");
map.put("Marson","Johnson");
map.put("Gnanabai","Johnson");
map.put("Johnson","Daniel");
map.put("Poomani Ammal","Daniel");
map.put("Gnanam","Daniel");
map.put("Daniel","Gnanam");
map.put("Dany","David");
map.put("Santhi","Daniel");
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
HashMap<String, String> copy = new HashMap<String, String>(map);
Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();
while (itr.hasNext()) {
int duplicate = 0;
Map.Entry<String, String> par = itr.next();
String value = par.getValue();
for (Map.Entry<String, String> pair : copy.entrySet()) {
if(pair.getValue().equals(value))
duplicate++;
if (duplicate > 1) {
removeItemFromMapByValue(map, value);
break;
}
}
}
}
public static void removeItemFromMapByValue(Map<String, String> map, String value) {
HashMap<String, String> copy = new HashMap<String, String>(map);
for (Map.Entry<String, String> pair : copy.entrySet()) {
if(pair.getValue().equals(value))
map.remove(pair.getKey());
}
}
public static void main(String[] args) {
}
}