Industry Ready Java Spring Boot, React & Gen AI — Live Course
JavaCollections

PriorityQueue

1. Introduction

PriorityQueue is a queue implementation in which elements are processed based on priority, not just insertion order.

By default, Java's PriorityQueue gives the smallest element first.

It is useful in:

  • Scheduling systems
  • Task prioritization
  • Dijkstra's algorithm
  • Heap-based problems

2. How PriorityQueue Differs from Normal Queue

A normal queue usually follows FIFO order.

A PriorityQueue does not. Instead, the element with the highest priority is removed first.

In Java's default behavior:

  • Smaller value means higher priority

Example:

import java.util.PriorityQueue;

public class Demo {
    public static void main(String[] args) {
        PriorityQueue<Integer> pq = new PriorityQueue<>();

        pq.offer(30);
        pq.offer(10);
        pq.offer(20);

        System.out.println(pq.poll());
        System.out.println(pq.poll());
        System.out.println(pq.poll());
    }
}

Output:

10
20
30

3. Internal Working

PriorityQueue is internally based on a heap. In Java, it is implemented as a min-heap by default.

Because of this:

  • Access to the highest priority element is efficient
  • Elements inside the queue are not fully sorted
  • Only the head element is guaranteed to be according to priority

4. Common Methods

MethodPurpose
offer()Insert element
add()Insert element
poll()Remove head element
peek()View head element
remove()Remove head or specific element

Example:

PriorityQueue<String> pq = new PriorityQueue<>();

pq.offer("Banana");
pq.offer("Apple");
pq.offer("Mango");

System.out.println(pq.peek());
System.out.println(pq.poll());
System.out.println(pq);

5. Natural Ordering

By default, PriorityQueue uses natural ordering.

Examples:

  • Integers: ascending order
  • Strings: alphabetical order

6. Max Heap with Comparator

If you want the largest element first, provide a comparator.

import java.util.Collections;
import java.util.PriorityQueue;

public class Demo {
    public static void main(String[] args) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());

        maxHeap.offer(30);
        maxHeap.offer(10);
        maxHeap.offer(20);

        System.out.println(maxHeap.poll()); // 30
        System.out.println(maxHeap.poll()); // 20
        System.out.println(maxHeap.poll()); // 10
    }
}

7. PriorityQueue with Custom Objects

You can use either Comparable or Comparator.

Example with Comparator:

import java.util.PriorityQueue;

class Task {
    String name;
    int priority;

    Task(String name, int priority) {
        this.name = name;
        this.priority = priority;
    }

    @Override
    public String toString() {
        return name + " - " + priority;
    }
}

public class Demo {
    public static void main(String[] args) {
        PriorityQueue<Task> tasks = new PriorityQueue<>(
            (t1, t2) -> t1.priority - t2.priority
        );

        tasks.offer(new Task("Bug Fix", 1));
        tasks.offer(new Task("Meeting", 3));
        tasks.offer(new Task("Code Review", 2));

        while (!tasks.isEmpty()) {
            System.out.println(tasks.poll());
        }
    }
}

8. Important Notes

8.1 Iteration is not sorted output

If you print a PriorityQueue directly, it may not look fully sorted.

PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(30);
pq.offer(10);
pq.offer(20);

System.out.println(pq);

The internal structure is heap-based, not a sorted list.

8.2 null values are not allowed

PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.offer(null); // exception

9. Time Complexity

OperationTime Complexity
offer()O(log n)
poll()O(log n)
peek()O(1)

10. Practical Example

import java.util.PriorityQueue;

public class HospitalQueue {
    public static void main(String[] args) {
        PriorityQueue<Integer> patients = new PriorityQueue<>();

        patients.offer(5);
        patients.offer(1);
        patients.offer(3);

        while (!patients.isEmpty()) {
            System.out.println("Treat patient priority: " + patients.poll());
        }
    }
}

Lower number here means higher treatment priority.

11. PriorityQueue vs Queue

FeatureNormal QueuePriorityQueue
OrderFIFOBased on priority
Internal logicSequentialHeap based
Head elementOldest insertedHighest priority

12. Summary

  • PriorityQueue processes elements according to priority.
  • By default, Java uses a min-heap.
  • offer(), poll(), and peek() are the main methods.
  • For reverse or custom priority, use a Comparator.
  • Iterating over the queue does not guarantee fully sorted output.

Written By: Shiva Srivastava

How is this guide?

Last updated on