One of requirements not completed "The removeFirstNameDuplicates() method must remove from the map all people who have the same first name.".
I can't find problem in "removeFirstNameDuplicates" methode, all work, but task not completed.
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
HashMap<String, String> map = new HashMap<String, String>();
//cycle for creat nine object in HashMap
for(int i = 0; i < 10; i++){
int num = i % 6;
String firstName = "Willy" + num;
String lastNam = "Bob" + i;
map.put(lastNam, firstName);
}
return map;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
String[] allStatVal = new String[10];
int p = 0;
//Cycle where all array from HashMap record to next to String arrays for more comfortable work
for(Map.Entry<String, String> pair : map.entrySet()){
String value = pair.getValue();
allStatVal[p] = value;
p++;
}
//cycle where we check does first name have duplicate (if yes delete it by key from string array)
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
if(i != j && allStatVal[i].equals(allStatVal[j])){
removeItemFromMapByValue(map, allStatVal[j]);
removeItemFromMapByValue(map, allStatVal[i]);
}
}
}
}
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) {
//Check how methode works
/*
HashMap<String, String> map = createMap();
for(HashMap.Entry<String, String>pair : map.entrySet()){
String key = pair.getKey();
String value = pair.getValue();
System.out.println(key + " - " + value);
}
removeFirstNameDuplicates(map);
for(HashMap.Entry<String, String>pair : map.entrySet()){
String key = pair.getKey();
String value = pair.getValue();
System.out.println(key + " - " + value + " 0000000000");
}
*/
}
}