Convert a Queue to List

后端 未结 6 1173
Happy的楠姐
Happy的楠姐 2020-12-31 01:29

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

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-31 01:52

    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.

提交回复
热议问题