I don't know why I am getting this error with an iterator. If I didn't have to use the other method I could probably solve this. Thanks for your help.
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
// initialize hashmap
HashMap<String, String> people = new HashMap<String, String>();
// put ten names in the HashMap (last name, first name)
people.put("Grilli", "Zach");
people.put("Lawson", "Cole");
people.put("Rickers", "Cole");
people.put("Middleton", "Kate");
people.put("Noonan", "Kate");
people.put("Fankell", "Jackie");
people.put("Onassus", "Jackie");
people.put("Rickersss", "Zach");
people.put("Lee", "Bruce");
people.put("Leee", "Bobby");
// return map
return people;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
// need to create an iterator
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String value = entry.getValue();
removeItemFromMapByValue(map, value);
}
// for (Map.Entry<String, String> pair : map.entrySet())
// {
// removeItemFromMapByValue(map, pair.getValue());
// }
}
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) {
// initialize map
HashMap<String, String> testMap = createMap();
// loop through map and print key value pairs
for (Map.Entry<String, String> pair : testMap.entrySet()) {
String key = pair.getKey();
String value = pair.getValue();
System.out.println(key + ": " + value);
}
System.out.println("------------------");
// call remove method
removeFirstNameDuplicates(testMap);
// print out names again
for (Map.Entry<String, String> pair : testMap.entrySet()) {
String key1 = pair.getKey();
String value1 = pair.getValue();
System.out.println(key1 + ": " + value1);
}
}
}
package com.codegym.task.task08.task0817;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
/*
We don't need repeats
*/
public class Solution {
public static HashMap<String, String> createMap() {
//write your code here
// initialize hashmap
HashMap<String, String> people = new HashMap<String, String>();
// put ten names in the HashMap (last name, first name)
people.put("Grilli", "Zach");
people.put("Lawson", "Cole");
people.put("Rickers", "Cole");
people.put("Middleton", "Kate");
people.put("Noonan", "Kate");
people.put("Fankell", "Jackie");
people.put("Onassus", "Jackie");
people.put("Rickersss", "Zach");
people.put("Lee", "Bruce");
people.put("Leee", "Bobby");
// return map
return people;
}
public static void removeFirstNameDuplicates(Map<String, String> map) {
//write your code here
// need to create an iterator
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
String value = entry.getValue();
removeItemFromMapByValue(map, value);
}
// for (Map.Entry<String, String> pair : map.entrySet())
// {
// removeItemFromMapByValue(map, pair.getValue());
// }
}
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) {
// initialize map
HashMap<String, String> testMap = createMap();
// loop through map and print key value pairs
for (Map.Entry<String, String> pair : testMap.entrySet()) {
String key = pair.getKey();
String value = pair.getValue();
System.out.println(key + ": " + value);
}
System.out.println("------------------");
// call remove method
removeFirstNameDuplicates(testMap);
// print out names again
for (Map.Entry<String, String> pair : testMap.entrySet()) {
String key1 = pair.getKey();
String value1 = pair.getValue();
System.out.println(key1 + ": " + value1);
}
}
}