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?
Since your key is a String you should remove String not Node. So try
_adjacentNodes.remove(node.getNodeID());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With