Convert a Queue to List

If you're converting from PriorityQueue to a List, remember that it is in fact a heap, so the ordering is determined using the poll() method, in which case, doing it by the constructor way as discussed in some of the other answers here, won't preserve the natural ordering of the queue.

Taking that into consideration, you can go along these lines:

List<E> result = new ArrayList<>(yourPriorityQueue.size());
while (!yourPriorityQueue.isEmpty()) {
    result.add(yourPriorityQueue.poll());
}

Pass Queue To ArrayList Constructor

The easiest way to just create a ArrayList and pass your Queue as an argument in the constructor of ArrayList that takes a Collection. A Queue is a Collection, so that works.

This is the easiest way and I believe fastest way too.

List<?> list = new ArrayList<>( myQueue );

The fastest is to use a LinkedList in the first place which can be used as a List or a Queue.

Queue q = new LinkedList();
List l = (List) q;

Otherwise you need to take a copy

List l = new ArrayList(q);

Note: When dealing with PriorityQueue, Use a loop, poll each element and add to list. PriorityQueue to List not maintaining the heap order.

Tags:

Java

Queue

List