How to iterate over a PriorityQueue?

后端 未结 9 2079
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 03:48
for (Event e : pq)

doesn\'t iterate in the priority order.

while(!pq.isEmpty()){
  Event e = pq.poll();
}

This wo

9条回答
  •  被撕碎了的回忆
    2020-11-30 04:42

    You can make a copy of the queue and poll in a loop, in this example pq is the original priority queue:

    PriorityQueue pqCopy = new PriorityQueue(pq);
    while(!pqCopy.isEmpty()){
        Your_Class obj = pqCopy.poll();
        // obj is the next ordered item in the queue
        .....
    }
    

提交回复
热议问题