What's wrong here? I have ConcurrentModificationException and I don't know how to repair that.
package com.codegym.task.task08.task0817;
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() {
//write your code here
HashMap<String, String> a = new HashMap<>();
a.put("lastName1", "firstName1");
a.put("lastName2", "firstName2");
a.put("lastName3", "firstName3");
a.put("lastName4", "firstName4");
a.put("lastName5", "firstName5");
a.put("lastName6", "firstName6");
a.put("lastName7", "firstName6");
a.put("lastName8", "firstName8");
a.put("lastName9", "firstName9");
a.put("lastName10", "firstName10");
return a;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
Map<String, String> b = new HashMap<>(map);
Iterator<Map.Entry<String, String>> remove = b.entrySet().iterator();
while (remove.hasNext()){
Map.Entry<String, String> a = remove.next();
int temp = 0;
String value = a.getValue();
Iterator<Map.Entry<String, String>> newRemove = map.entrySet().iterator();
while (newRemove.hasNext()){
Map.Entry<String, String> c = newRemove.next();
if(value.equals(c.getValue())){
temp++;
}
if(temp > 1){
removeItemFromMapByValue(map, value);
}
}
}
}
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) {
}
}