Introduction to Priority Queue in Java
Priority Queue Introduction in Java
Priority Queue Introduction in Java explains a special type of queue where elements are processed based on priority instead of insertion order. Unlike a normal queue that follows FIFO (First In, First Out), a priority queue removes the element with the highest or lowest priority first, depending on the implementation.
Java provides a built in implementation of priority queues through the PriorityQueue class in the Java Collections Framework, located in the java.util package.
What is a Priority Queue?
Priority Queue is a data structure where each element has an associated priority. The element with the highest priority is removed first.
Unlike a normal queue:
| Queue Type | Removal Order |
|---|---|
| Normal Queue | FIFO |
| Priority Queue | Based on Priority |
Example:
Suppose we insert the following values into a priority queue:
30, 10, 50, 20
If it is a min priority queue (default in Java):
10 will be removed first
Order of removal:
10 → 20 → 30 → 50
1. CPU scheduling
2. Dijkstra’s shortest path algorithm
3. Huffman coding
4. Event driven simulations
5. Network routing
6. Task scheduling systems
How Priority Queue Works in Java ?
Java implements priority queues internally using a Binary Heap.
- Implemented as a min heap by default
- The smallest element always appears at the head
- Not strictly sorted internally
- Only the head element is guaranteed to be the smallest
Basic Operations of Priority Queue
| Operation | Description |
|---|---|
| add() / offer() | Insert element |
| remove() / poll() | Remove highest priority element |
| peek() | View highest priority element |
| size() | Number of elements |
| isEmpty() | Check if empty |
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Java Program for Priority Queue
import java.util.PriorityQueue;
public class PriorityQueueDemo {
public static void main(String[] args) {
PriorityQueue pq = new PriorityQueue<>();
// Insert elements
pq.offer(30);
pq.offer(10);
pq.offer(50);
pq.offer(20);
System.out.println("Priority Queue: " + pq);
// Peek
System.out.println("Head element: " + pq.peek());
// Remove elements
System.out.println("Removed: " + pq.poll());
System.out.println("Removed: " + pq.poll());
System.out.println("Priority Queue after removals: " + pq);
}
}
Input:
Insert: 30 Insert: 10 Insert: 50 Insert: 20
Output:
Priority Queue: [10, 20, 50, 30] Head element: 10 Removed: 10 Removed: 20 Priority Queue after removals: [30, 50]
Space Complexity: O(n)
Types of Priority Queue
1. Min Priority Queue
The smallest element has the highest priority.
Example: 10, 20, 30, 40
2. Max Priority Queue
The largest element has the highest priority. This can be implemented using a comparator.
Example: 40, 30, 20, 10
Priority Queue vs Normal Queue:
| Feature | Queue | Priority Queue |
|---|---|---|
| Order | FIFO | Priority based |
| Structure | Linear | Heap |
| Removal | First inserted | Highest priority |
| Complexity | O(1) | O(log n) |
Frequently Asked Questions
Answer:
A priority queue is a data structure where elements are processed based on priority rather than insertion order.
Answer:
Java PriorityQueue is implemented using a binary heap.
Answer:
Insertion and deletion take O(log n), while peek takes O(1).
Answer:
Java PriorityQueue is a min heap by default.
Answer:
Use a comparator such as Collections.reverseOrder().
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Stacks
- Introduction to Stack in Data Structure
Click Here - Operations on a Stack
Click Here - Stack: Infix, Prefix and Postfix conversions
Click Here - Stack Representation in –
C | C++ | Java - Representation of a Stack as an Array. –
C | C++ | Java - Representation of a Stack as a Linked List. –
C | C++ | Java - Infix to Postfix Conversion –
C | C++ | Java - Infix to prefix conversion in –
C | C++ | Java - Postfix to Prefix Conversion in –
C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
Click Here - Queues Program in C and implementation
Click Here - Implementation of Queues using Arrays | C Program
Click Here - Types of Queues in Data Structure
Click Here - Application of Queue Data Structure
Click Here - Insertion in Queues Program (Enqueuing) –
C | C++ | Java - Deletion (Removal) in Queues Program(Dequeuing) –
C | C++ | Java - Reverse a Queue –
C | C++ | Java - Queues using Linked Lists –
C | C++ | Java - Implement Queue using Stack –
C | C++ | Java - Implement Queue using two Stacks –
C | C++ | Java
Circular Queues
- Circular queue in Data Structure
Click Here - Applications of Circular Queues
Click Here - Circular queue in –
C | C++ | Java - Circular queue using Array –
C | C++ | Java - Circular Queue using Linked Lists –
C | C++ | Java
Priority Queue
Stacks
- Introduction to Stack in Data Structure
- Operations on a Stack
- Stack: Infix, Prefix and Postfix conversions
- Stack Representation in – C | C++ | Java
- Representation of a Stack as an Array. – C | C++ | Java
- Representation of a Stack as a Linked List. – C | C++ | Java
- Infix to Postfix Conversion – C | C++ | Java
- Infix to prefix conversion in – C | C++ | Java
- Postfix to Prefix Conversion in – C | C++ | Java
Queues
- Queues in Data Structures (Introduction)
- Queues Program in C and implementation
- Implementation of Queues using Arrays | C Program
- Types of Queues in Data Structure
- Application of Queue Data Structure
- Insertion in Queues Program (Enqueuing) – C | C++ | Java
- Deletion (Removal) in Queues Program(Dequeuing) – C | C++ | Java
- Reverse a Queue – C | C++ | Java
- Queues using Linked Lists – C | C++ | Java
- Implement Queue using Stack – C | C++ | Java
- Implement Queue using two Stacks – C | C++ | Java
Circular Queues
- Circular queue in Data Structure
- Applications of Circular Queues
- Circular queue in – C | C++ | Java
- Circular queue using Array – C | C++ | Java
- Circular Queue using Linked Lists – C | C++ | Java

Login/Signup to comment