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.

priority queue in java

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 TypeRemoval Order
Normal QueueFIFO
Priority QueueBased 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

How Priority Queue Works in Java ?

Java implements priority queues internally using a Binary Heap.

  1. Implemented as a min heap by default
  2. The smallest element always appears at the head
  3. Not strictly sorted internally
  4. Only the head element is guaranteed to be the smallest
priority queue insertion in java

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
priority queue deletion in java

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Algorithm for Priority Queue in Java

Insert Element

1. Insert element at the end of heap
2. Compare with parent
3. Swap if smaller (min-heap)
4. Repeat until heap property satisfied

Remove Element

1. Remove root element
2. Move last element to root
3. Heapify down
4. Restore heap property

Java Program for Priority Queue

Run
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]

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:

FeatureQueuePriority Queue
OrderFIFOPriority based
StructureLinearHeap
RemovalFirst insertedHighest priority
ComplexityO(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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

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

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction –
    C | C++ | Java
  • Priority Queue Implementation using Array –
    C | C++ | Java
  • Priority Queue using Linked List –
    C | C++ | Java
  • Priority Queue Insertion and Deletion-
    C | C++ | Java

Stacks

Queues

Circular Queues

Priority Queue

  • Application of Priority Queue
  • Priority Queue Example
  • Priority Queue Introduction – C | C++ | Java
  • Priority Queue Implementation using Array – C | C++ | Java
  • Priority Queue using Linked List – C | C++ | Java
  • Priority Queue Insertion and Deletion- C | C++ | Java