Java PriorityQueue peek() Method

The PriorityQueue class in Java provides the peek() method to retrieve, but not remove, the head of the queue.

Table of Contents

  1. Introduction
  2. peek Method Syntax
  3. Examples
    • Retrieving the Head of the PriorityQueue
    • Handling an Empty PriorityQueue
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The PriorityQueue.peek() method is used to retrieve the head of the PriorityQueue without removing it. The head of the queue is the smallest element according to the natural ordering or the comparator provided. If the queue is empty, the method returns null.

peek Method Syntax

The syntax for the peek method is as follows:

public E peek()
  • The method does not take any parameters.
  • The method returns the head of the queue, or null if the queue is empty.

Examples

Retrieving the Head of the PriorityQueue

The peek method can be used to view the head element of a PriorityQueue.

Example

import java.util.PriorityQueue;

public class PriorityQueuePeekExample {
    public static void main(String[] args) {
        // Creating a PriorityQueue of Strings
        PriorityQueue<String> tasks = new PriorityQueue<>();

        // Adding elements to the PriorityQueue
        tasks.add("Complete project report");
        tasks.add("Email client updates");
        tasks.add("Prepare presentation");

        // Retrieving the head of the PriorityQueue using peek
        String headTask = tasks.peek();

        // Printing the head of the PriorityQueue
        System.out.println("Head of the PriorityQueue: " + headTask);
    }
}

Output:

Head of the PriorityQueue: Complete project report

Handling an Empty PriorityQueue

When the PriorityQueue is empty, the peek method returns null.

Example

import java.util.PriorityQueue;

public class EmptyPriorityQueuePeekExample {
    public static void main(String[] args) {
        // Creating an empty PriorityQueue of Strings
        PriorityQueue<String> tasks = new PriorityQueue<>();

        // Retrieving the head of the empty PriorityQueue using peek
        String headTask = tasks.peek();

        // Printing the head of the empty PriorityQueue
        System.out.println("Head of the empty PriorityQueue: " + headTask);
    }
}

Output:

Head of the empty PriorityQueue: null

Real-World Use Case

Use Case: Task Management System

In a task management system, you may need to check the highest-priority task without removing it from the queue. The peek method can be used to view the next task to be processed.

Example

import java.util.PriorityQueue;

public class TaskManagementSystem {
    public static void main(String[] args) {
        // Creating a PriorityQueue to store tasks
        PriorityQueue<Task> tasks = new PriorityQueue<>();

        // Adding initial tasks with different priorities
        tasks.add(new Task("Complete project report", 2));
        tasks.add(new Task("Email client updates", 1));
        tasks.add(new Task("Prepare presentation", 3));

        // Retrieving the head task using peek
        Task nextTask = tasks.peek();

        // Printing the next task to be processed
        System.out.println("Next task to be processed: " + nextTask);
    }
}

class Task implements Comparable<Task> {
    private String description;
    private int priority;

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

    @Override
    public int compareTo(Task other) {
        return Integer.compare(this.priority, other.priority);
    }

    @Override
    public String toString() {
        return description + " (Priority: " + priority + ")";
    }
}

Output:

Next task to be processed: Email client updates (Priority: 1)

Conclusion

The PriorityQueue.peek() method in Java is used for accessing the head element of a priority queue without removing it. Understanding how to use this method allows you to effectively manage and view the highest-priority elements in the queue, making it particularly useful in applications like task management systems where you need to check the next task to be processed without altering the queue.

Comments