Not sure why my removeFirstNameDuplicates method doesn't remove duplicate values.
package com.codegym.task.task08.task0817;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
HashMap<String,String> newMap = new HashMap<>();
newMap.put("Pace","June");
newMap.put("Jones", "Rachel");
newMap.put("Bloomquist", "Rachel");
newMap.put("Smith","John");
newMap.put("Green","Bob");
newMap.put("Black", "Jack");
newMap.put("Brown","Sam");
newMap.put("Smack", "Joe");
newMap.put("Hork","John");
newMap.put("Berk","Jerm");
return newMap;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
ArrayList<String> valuesCopyList = new ArrayList<>();
for (Map.Entry mapElement : map.entrySet()) {
String value = (String) mapElement.getValue();
valuesCopyList.add(value);
}
for (Map.Entry mapElement2 : map.entrySet()) {
String value2 = (String)mapElement2.getValue();
for(String listElement: valuesCopyList){
if(listElement.equals(value2)){
removeItemFromMapByValue(map,value2);
}
}
}
}
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) {
}
}