Java Priority Queue: PriorityQueue With Comparator Examples
A Java priority queue (java.util.PriorityQueue) always returns the smallest element first, or the first element by a Comparator. Learn natural and custom order, max-heaps, ties and why a printed queue looks unsorted.
![A binary min-heap with 1 at the root, 2 and 8 as its children, and 5 and 3 as the children of 2, next to the backing array [1, 2, 8, 5, 3] with indexes 0 to 4. toString() and the iterator read the array in that order, while calling poll() until the queue is empty returns 1, 2, 3, 5, 8.](/sitelet?url=https%3A%2F%2Fhowtodoinjava.com%2Fwp-content%2Fuploads%2F2026%2F10%2Fjava-priorityqueue-binary-heap-array-order-768x432.png)