1

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.

Jay Lee
  • 23
  • 4
  • Sorry, I have closed your question since we already have an identical one here. If you meant something more specific, please edit the post to clarify it, and we will reopen it. – Andrew Tobilko May 07 '18 at 10:54
  • @Andrew I can't get my head around how to loop `while(it.hasNext())` twice to get the people_attributes hashmap – Jay Lee May 07 '18 at 11:27
  • what did you mean by "twice"? writing two consecutive `while` statements? – Andrew Tobilko May 08 '18 at 12:42

0 Answers0