So I've made it so I'm not removing from the loop I'm iterating through in addition to adding a nested loop that removes if the string is the same. How would I go about fixing this? I believe I'm on my 10th or so attempt.
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> names = new HashMap<String, String>();
names.put("Smith", "John");
names.put("Sorel", "Ani");
names.put("Kiributa", "COD");
names.put("James", "Jesse");
names.put("RS", "Settled");
names.put("Sorni", "Pilia");
names.put("Gono", "Forsen");
names.put("Anime", "Jesse");
names.put("Fun", "Spongebob");
names.put("MOD", "COD");
return names;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
HashMap<String, String> newMap = new HashMap<String, String>(map);
Iterator<Map.Entry<String, String>> iter = newMap.entrySet().iterator();
while(iter.hasNext()){
Map.Entry<String, String> entry = iter.next();
String s = entry.getValue();
while(iter.hasNext()){
Map.Entry<String, String> oNext = iter.next();
String n = oNext.getValue();
if(s.equals(n)){
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());
}
}
public static void main(String[] args) {
}
}