Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java: how to addAll(Collection<>) to the front of the queue?

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?

like image 743
KJW Avatar asked Oct 30 '11 08:10

KJW


People also ask

Does list addAll maintain order?

The new elements will appear in the list in the order that they are returned by the specified collection's iterator.

Does a queue add to the front?

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.


1 Answers

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
like image 190
azerafati Avatar answered Oct 26 '22 21:10

azerafati