I currently have a problem with removing a key in a hashmap. I created a hashmap within a hashmap. I need to remove a key by the value inside the hashmap within the hashmap. So the hashmap looks like this:
HashMap<String, String> people_attributes = new HashMap<String, String>();
Map<String, HashMap<String, String>> people = new HashMap<String, HashMap<String, String>>();
If I try to remove a key, replace remove_name.add(p_name)
with people.remove(p_name)
I get java.util.ConcurrentModificationException
.
Right now, I use an arraylist to add the keys that needs to be remove, then loop through the arraylist to remove the keys from the hashmap.
So far this is the solution:
for(String p_name : people.keySet()) { // search through people with attribute
for(String p_attributes : people.get(p_name).keySet()) { // search through attributes map
if(p_attributes.equals(g_att)) { // when current attributes equal to guess attribute
p_value = people.get(p_name).get(p_attributes);
if(!p_value.equals(g_value)) { // if current value doesn't equal to guess value
remove_name.add(p_name);
}
}
}
}
for(String r_name : remove_name) {
people.remove(r_name);
}
EDIT:
Problem: I know I can use iterator, like all the other questions ask in stackoverflow, but I don't know how to loop twice to get into people_attributes hashmap.