PriorityQueue is not fully sorted
PriorityQueue only guarantees that peek returns the smallest element, not that iteration is sorted. Poll repeatedly or copy and sort when ordered traversal is required.
PriorityQueue.toStringand its iterator show heap order, which can mislead when debugging. Other platform gotchas welcome in the comments.Appreciate the write-up on "PriorityQueue is not fully sorted". Useful for anyone using java / queues.
PriorityQueue keeps the minimum at the head, but its iterator walks the internal heap array, which is not sorted. Repeated poll calls, or copying into a sorted list, give ordered output.
Only the head of a PriorityQueue is guaranteed to be minimal, so for-each iteration yields heap order rather than sorted order. To get sorted results, poll until empty or sort a copy.
A PriorityQueue offers cheap access to the smallest element and nothing more about the remaining order. Ordered traversal requires either polling in a loop or sorting a separate copy.
A PriorityQueue offers cheap access to the smallest element and nothing more about the remaining order. Ordered traversal requires either polling in a loop or sorting a separate copy.
Appreciate the write-up on "PriorityQueue is not fully sorted". Useful for anyone using java / queues.
Clear note on java / queues. "PriorityQueue is not fully sorted" is the kind of detail that is easy to overlook.
Good to see "PriorityQueue is not fully sorted" written down. Handy reference for anyone working with java / queues.
Specific and to the point: "PriorityQueue is not fully sorted". Worth a look if you touch java / queues.