Java HashMap Removing Key/Value

Viewed 91263

I'm just looking for an explanation and/or insight as to why its better to iterate over a HashMap.

For instance the code below (in my eyes) does the exact same (or it should). However if I don't iterate over the HashMap the key is not removed.

_adjacentNodes.remove(node);        

Iterator<Map.Entry<String, LinkedList<Node>>> iterator = _adjacentNodes.entrySet().iterator();
while (iterator.hasNext()) {
     Map.Entry<String, LinkedList<Node>> entry = iterator.next();
     if(node.getNodeID().contentEquals(entry.getKey())){
          iterator.remove();
     }
}

What is going on?

3 Answers
Related