Java PriorityQueue
Java PriorityQueue
A PriorityQueue is a special kind of queue where elements with the highest priority are removed first - not necessarily the ones added first.
It is part of the java.util package and implements the Queue interface.
Tip: Think of a PriorityQueue like a hospital emergency room: the most urgent patients are treated first, even if they arrived later.
Default behavior: The smallest element (natural ordering) is given the highest priority. For example, in a queue of numbers 40, 10, 30, the number 10 is removed first.
Create a PriorityQueue
Example
Create a PriorityQueue object called numbers that will store integers:
import java.util.PriorityQueue;
PriorityQueue<Integer> numbers = new PriorityQueue<>();
Now you can use methods like add() to insert elements, peek() to look at the top priority, and poll() to remove it.
Add Elements
Use add() or offer() to insert elements:
Example
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
// Create a PriorityQueue of integers
PriorityQueue<Integer> numbers = new PriorityQueue<>();
// Add some elements
numbers.add(40);
numbers.add(10);
numbers.add(30);
numbers.add(20);
System.out.println(numbers);
}
}
Peek and Poll
Use peek() to look at the smallest element (highest priority),
and poll() to remove it:
Example
numbers.add(10);
numbers.add(30);
numbers.add(30);
numbers.add(20);
System.out.println("Top priority: " + numbers.peek());
numbers.poll(); // Remove the smallest
System.out.println("After poll: " + numbers);
Loop Through a PriorityQueue
You can use a for-each loop to go through the elements. Note that
the iteration order is not necessarily sorted. It is because a PriorityQueue
maintains priority when removing elements, not when looping:
Example
numbers.add(40);
numbers.add(1);
numbers.add(10);
numbers.add(30);
numbers.add(20);
numbers.add(50);
// Loop through the elements
for (int n : numbers) {
System.out.println(n);
}
Result:
1
20
10
40
30
50
If you want sorted order while printing, use a while loop with poll() instead - because poll() removes and returns the smallest element each time, giving you elements in priority order:
Example
numbers.add(40);
numbers.add(1);
numbers.add(10);
numbers.add(30);
numbers.add(20);
numbers.add(50);
// Print elements in sorted order (by priority)
while (!numbers.isEmpty()) {
System.out.println(numbers.poll());
}
Result:
1
10
20
30
40
50
Use a for-each loop if you just want to see the contents (in any order).
Use a while loop with poll() to retrieve elements in sorted priority order.
PriorityQueue with Strings
You can also use PriorityQueue with strings:
Example
import java.util.PriorityQueue;
public class Main {
public static void main(String[] args) {
PriorityQueue<String> names = new PriorityQueue<>();
names.add("John");
names.add("Bo");
names.add("Liam");
names.add("Angie");
while (!names.isEmpty()) {
System.out.println(names.poll());
}
}
}
Result:
Angie
Bo
John
Liam
When to Use PriorityQueue?
Use a PriorityQueue when you need elements to be processed by importance rather than the order they were added.
Some common use cases:
- Task scheduling (e.g., urgent jobs handled first)
- Managing print queues by urgency
- Simulating priority-based systems (like airline boarding)