public void traverse(Node root){
ArrayDeque<Node> queue = new ArrayDeque<Node>();
queue.add(root);
while(!queue.isEmpty()){
Node currentNode = queue.pollFirst();
List<Node> nl = getChildrenfromDB(currentNode);
queue.addAll(nl);
}
so should I be using ArrayDeque
or LinkedList
or LinkedBlockingDeque
? what would happen when I set int value of 10? Does this mean the queue will only hold 10 at a time? what if the Collection retrieved from DB's size is larger than 10? Is this bound value define the "snapshot" of the queue?
public void traverse(Node root){
LinkedBlockingDeque<Node> queue = new LinkedBlockingDeque<Node>(10);
queue.add(root);
while(!queue.isEmpty()){
Node currentNode = queue.pollFirst();
List<Node> nl = getChildrenfromDB(currentNode);
queue.addAll(nl);
}
Don't use an
ArrayList
-- use one of the implementations of theDeque
interface. For example, use aLinkedBlockingDeque
, which is designed for this sort of thing. Use theaddFirst()
,addLast()
,pollFirst()
, andpollLast()
methods as needed.If, for some reason, you really need to use a
List
, use aLinkedList
-- adding to the front of anArrayList
is extremely inefficient, as all elements need to be moved.