How to iterate over a PriorityQueue?

后端 未结 9 2068
被撕碎了的回忆
被撕碎了的回忆 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:25

    From the Javadocs

    The Iterator provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

    There are probably other equivalent mechanisms.

提交回复
热议问题