What is the fastest way to convert a Queue into a List while keeping the Queue order?
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.
Queue To ArrayList ConstructorThe 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 );
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