Consider the function below:
private static void printTrie(Node root){
Iterator it = root.children.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println((Character)pair.getKey());
root = (Node)pair.getValue();
printTrie(root);
it.remove(); // avoids a ConcurrentModificationException
}
}
Here's the definition of Node:
static class Node {
private HashMap<Character, Node> children;
private boolean isCompleteContact;
public Node(){
children = new HashMap<Character, Node>();
isCompleteContact = false;
}
}
My question is this: in the function I create an Iterator called it based on root. However, halfway through iterating over all the elements exposed by it, I reassign root to another node. Does this change the behaviour of the it? I.e., does it still refer to the original root or the new value we assign to root halfway through the while loop?