I don t see why removeFirstNameDuplicates(Map<String, String> map) works. Pls, i realy need an explanation! Tk you! public static void removeFirstNameDuplicates(Map<String, String> map) { map = {"ab"-"ab", "ac"-"ac", "ad"-"ab", "ae"-"ab"...} // let say ArrayList<String> list = new ArrayList<>(); // empty array ArrayList<String> list1 = new ArrayList<>(); // empty array for(String s: map.values()){ if(!list.contains(s)){ // if list is empty then first "ab"(value) is not yet in the list, so list.add(s) will be the next step(for passing the task we must delete from map all repeting value(all "ab") list.add(s); //"ab", "ac" }else list1.add(s);//"ab"(from "ad"-"ab") and "ab"(from "ae"-"ab") } for(String s : list1){ removeItemFromMapByValue(map, s); } } 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()); } } }