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?
EntrySetand remove actual value inHashMap(_adjacentNodes). Much more conveneint is to iterate overKeySet. The condition will be little more complicated (...'node.getNodeID().contentEquals(_adjacentNodes.get(entry.getKey()).getNodeID())'...), but finding the value from the HashMap should be quick.