Question:
I want to loop through aPriorityQueue
, and wondering if following iterator()
is the best wayI don’t care about the queue afterwards, so another way would be to use
poll()
while !queue.isEmpty()
but the time complexity would be O(logn).Answer:
If you want ordered retrieval of your elements, you must poll/remove elements from the queue; using iterator will not be enough.If you have better answer, please add a comment about this, thank you!