PriorityQueue is a class in java.util that implements an unbounded queue backed by a binary min-heap. Each call to poll() or remove() returns the element with the highest priority, which by default means the smallest according to natural ordering or a Comparator you supply. It is not a FIFO queue and it does not keep its elements fully sorted.
It is the standard choice in Java for task scheduling, "top K" problems, Dijkstra's algorithm and event simulations.
A PriorityQueue stores its elements in an array that represents a binary heap. The heap guarantees only one thing: the head of the queue is the least element. The remaining elements are arranged so that every parent is less than or equal to its children, which is why iterating over the queue or printing it does not give sorted output.
Elements must be mutually comparable. If they do not implement Comparable and you do not pass a Comparator, add() throws a ClassCastException. null elements are not allowed and throw a NullPointerException.
Pass a Comparator to the constructor to change the order. A max-heap, for example, is built by reversing the natural order. For your own types, Comparator.comparing keeps the code short and readable.
Add a tie-breaker such as thenComparing when equal priorities must come out in a stable order, because PriorityQueue does not guarantee FIFO order among equal elements.
| Method | Behaviour | Time complexity |
|---|---|---|
add(e) / offer(e) | Inserts the element and sifts it up | O(log n) |
peek() | Returns the head, or null if empty | O(1) |
element() | Returns the head, throws NoSuchElementException if empty | O(1) |
poll() | Removes and returns the head, or null if empty | O(log n) |
remove() | Removes and returns the head, throws if empty | O(log n) |
remove(Object o) / contains(Object o) | Linear search | O(n) |
new PriorityQueue<>(collection) | Builds the heap from a collection | O(n) |
The queue is unbounded and grows as needed, so offer() always succeeds apart from running out of memory. The default initial capacity is 11; you can pass a larger capacity to the constructor when you know the size in advance.
toString() expose the internal heap order, not sorted order. To read elements in priority order, call poll() repeatedly or copy to a list and sort it.PriorityQueue is not thread-safe. Use java.util.concurrent.PriorityBlockingQueue when several threads share the queue.Integer.compare(a, b) rather than a - b in a comparator, since subtraction can overflow.TreeSet or an indexed heap.A min-heap of size K is the classic way to find the K largest items in a stream using O(K) memory. The smallest of the current top K sits at the head and is evicted when something larger arrives.
This runs in O(n log k), which beats sorting the whole input when k is much smaller than n.
It is a min-heap by default. Pass Comparator.reverseOrder() or a custom Comparator to get max-heap behaviour.
No. Only the head is guaranteed to be the smallest. The rest follow the heap invariant, so iteration order is not sorted.
No. Use PriorityBlockingQueue for concurrent producers and consumers, or synchronise access yourself.
Duplicates are allowed. null is rejected with a NullPointerException.
PriorityQueue is Java's binary-heap queue: insertion and removal cost O(log n), peeking costs O(1), and the head is always the least element by natural order or by your Comparator. Remember that it is unsorted when iterated, not thread-safe, and sensitive to mutation of elements after insertion.