PriorityQueue:
PriorityQueue works internally in the Java Collection Framework:
PriorityQueue as follows: PriorityQueue<Integer> pq = new PriorityQueue<>();
PriorityQueue object is created internally using a min-heap (implemented via a dynamic array).
Initially, it is empty and ready to store elements.
_____________________________
| | | | | |
-----------------------------
(Index positions of internal array)
add() or offer() method:pq.add(30);pq.add(10);pq.add(20);pq.add(40);
(30)
Internal array:
______________________
| 30 | | | |
------------------------
(Index: 0)
(10)
/
(30)
Internal array:
______________________
| 10 | 30 | | |
------------------------
(Index: 0 1)
(10)
/ \
(30) (20)
Internal array:
__________________________
| 10 | 30 | 20 | |
--------------------------
(Index: 0 1 2)
(10)
/ \
(30) (20)
/
(40)
Internal array representation:
______________________________________
| 10 | 30 | 20 | 40 | | |
--------------------------------------
(Index: 0 1 2 3)
peek()):
peek() method retrieves the head element of the PriorityQueue without removing it.
System.out.println(pq.peek()); // Output β 10
______________________________________
| 10 | 30 | 20 | 40 | | |
--------------------------------------
β
Head element (smallest)
poll()):
poll() method removes and returns the head element from the PriorityQueue.
System.out.println(pq.poll()); // Output β 10
______________________________________
| 10 | 30 | 20 | 40 | | |
--------------------------------------
β
Head element (to be removed)
______________________________________
| 20 | 30 | 40 | | | |
--------------------------------------
β
New head (smallest after reordering)
PriorityQueue automatically maintains its heap structure internally after each removal.
PriorityQueue arranges elements in ascending order (min-heap).
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
(40)
/ \
(30) (20)
/
(10)
Internal array representation:
______________________________________
| 40 | 30 | 20 | 10 | | |
--------------------------------------
(Index: 0 1 2 3)
PriorityQueue in Java is not thread-safe. If multiple threads modify it concurrently, it must be synchronized externally.
PriorityBlockingQueue (from java.util.concurrent) if thread-safety is required.
import java.util.concurrent.PriorityBlockingQueue;
PriorityBlockingQueue<Integer> pq = new PriorityBlockingQueue<>();
pq.add(40);
pq.add(10);
pq.add(30);
System.out.println(pq.take()); // 10 β smallest element
PriorityQueue is Good for:
PriorityQueue automatically arranges elements in their natural order (for Comparable objects) or based on a custom Comparator.
peek() and poll() always operate on the head element (the smallest or highest priority).
PriorityQueue is a Min-Heap.
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
PriorityQueue is Not Good for:
PriorityQueue does not allow efficient random access to elements.
PriorityQueue is not thread-safe.
PriorityBlockingQueue for concurrent or multithreaded environments.
null throws a NullPointerException.
null cannot be compared with other elements.
| Operation | Description | Performance / Complexity |
|---|---|---|
add(E e) / offer(E e) |
Inserts the specified element and maintains heap order (heapify-up). | O(log n) |
peek() |
Retrieves (but does not remove) the head element β the smallest or highest priority. | O(1) |
poll() |
Removes and returns the head element, then heapifies down to restore order. | O(log n) |
remove(Object o) |
Removes a specific element if present (linear search required). | O(n) |
contains(Object o) |
Checks whether the queue contains a specific element. | O(n) |
size() |
Returns the number of elements in the queue. | O(1) |
iterator() |
Returns an iterator over the elements β but not in any guaranteed order. | O(n) |
Thread Safety |
Not thread-safe (use PriorityBlockingQueue for concurrent use). |
Synchronization required externally |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weβre here to make our tutorials better based on your thoughts and suggestions.