A priority queue in Java is a type of data structure that orders elements by their priority, so elements with higher (or lower) priority are processed first. Java provides a PriorityQueue class as part of the java.util package, which uses a min-heap by default, meaning the element with the smallest priority value is at the head of the queue. It’s a useful structure for managing tasks where specific elements need to be prioritized, such as job scheduling or pathfinding algorithms.

Definition

In Java, a priority queue is an abstract data type that processes elements based on their priority rather than their order of insertion. This priority-based ordering is handled internally by a binary heap, and elements can be either sorted in ascending order (for min-heap) or descending order (for max-heap, achieved using a comparator).

Example:

import java.util.PriorityQueue;

class Job {
    String name;
    int priority;

    // Constructor to initialize the job with name and priority
    public Job(String name, int priority) {
        this.name = name;
        this.priority = priority;
    }

    // Override toString method to display job details
    @Override
    public String toString() {
        return "Job{name='" + name + "', priority=" + priority + '}';
    }
}

public class PriorityQueueExample {

    public static void main(String[] args) {
        // Create a priority queue with a custom comparator (min-priority queue)
        PriorityQueue<Job> jobQueue = new PriorityQueue<>((a, b) -> a.priority - b.priority);
        
        // Add jobs to the queue
        jobQueue.add(new Job("Job1", 3));  // Lower priority
        jobQueue.add(new Job("Job2", 1));  // Higher priority
        jobQueue.add(new Job("Job3", 2));  // Medium priority
        
        // Process jobs based on priority
        System.out.println("Job Processing Order:");
        while (!jobQueue.isEmpty()) {
            System.out.println(jobQueue.poll());  // Dequeue and process job with highest priority
        }
    }
}

Output:

Job Processing Order:
Job{name='Job2', priority=1}
Job{name='Job3', priority=2}
Job{name='Job1', priority=3}

Features

  1. Elements are dequeued based on priority rather than insertion order.
  2. The element with the highest priority (or lowest priority number in this case) is quickly accessible.
  3. Allows dynamic prioritization where priorities can be modified and reordered.
  4. Java’s PriorityQueue can be customized with a comparator to change the ordering criteria (min-heap or max-heap).

Advantages

  1. Tasks are efficiently scheduled based on priority, suitable for scenarios like CPU scheduling.
  2. Retrieves the highest-priority element in constant time (O(1) for retrieval, O(log n) for insertion).
  3. Suitable for real-time applications that require immediate handling of critical tasks, such as real-time simulations or operating systems.
  4. Allows for custom comparators, making it easy to switch between different priority criteria, such as lowest or highest priority.