29

What is the fastest way to convert a Queue into a List while keeping the Queue order?

MBZ
  • 26,084
  • 47
  • 114
  • 191
  • `q.poll(), list.add()` ? – Nishant Oct 08 '12 at 10:28
  • 3
    FYI this has issues when used with [PriorityQueue](https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html) read this [Hacker Rank Case](https://www.hackerrank.com/challenges/java-priority-queue/forum/comments/487333 ) you will have to poll with loop to get it right in the list – shareef Sep 02 '18 at 20:00
  • @shareef I'm having the same issue. Took the whole evening for an actually easy problem. – Minh Nghĩa Apr 12 '19 at 17:52

6 Answers6

33

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.

Sundeep1501
  • 1,510
  • 1
  • 18
  • 28
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
12

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 );
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Pankaj Goyal
  • 950
  • 9
  • 15
  • 2
    Should be the accepted answer, is it is the only one to directly address the Question. The Question referred to the [`Queue`](http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html) interface. There are many implementations of Queue, both bundled with Java and found in other libraries such as [Google Guava](https://github.com/google/guava). The other answers refer only to [`LinkedList`](http://docs.oracle.com/javase/8/docs/api/java/util/LinkedList.html) which is but one of many implementations. – Basil Bourque Jan 14 '15 at 04:35
12

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());
}
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
Minh Nghĩa
  • 854
  • 1
  • 11
  • 28
3
Queue queue = new LinkedList();
...
List list = new ArrayList(queue);
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
2

Answering to old question for users who are already on java 8
Java 8 provides the option of using streams and you can get a list from queue as:

For example:

Queue<Student> queue = new LinkedList<>();
        Student s1 = new Student("A",2);
        Student s2 = new Student("B",1);
        Student s3 = new Student("C",3);
        queue.add(s1);
        queue.add(s2);
        queue.add(s3);

    List<Student> studentList = queue.stream().collect(Collectors.toCollection(ArrayList::new));
nilesh_101
  • 307
  • 3
  • 10
1

Google:

Queue fruitsQueue = new LinkedList();
fruitsQueue.add("Apples");
fruitsQueue.add("Bananas");
fruitsQueue.add("Oranges");
fruitsQueue.add("Grapes");

List fruitsList = new ArrayList(fruitsQueue);
User404
  • 2,152
  • 2
  • 27
  • 35