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);
}
how would I get addAll(nl)
to add the entire collection(List<Node>
) to the front of the queue?
The new elements will appear in the list in the order that they are returned by the specified collection's iterator.
Queues are data structures that follow the First In First Out (FIFO) i.e. the first element that is added to the queue is the first one to be removed. Elements are always added to the back and removed from the front. Think of it as a line of people waiting for a bus.
Actually I was looking for the same thing, and this worked for me!!
samplelist.addAll(0,items); // 0 is the index where items are added on the list
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