Java ArrayDeque peek() Method

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

Table of Contents

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

Introduction

The ArrayDeque.peek() method is used to retrieve, but not remove, the head of the ArrayDeque. If the deque is empty, the method returns null instead of throwing an exception, making it a safe way to access the first element without modifying the deque.

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 deque, or null if the deque is empty.

Examples

Retrieving the Head of the ArrayDeque Using peek

The peek method can be used to view the head element of an ArrayDeque.

Example

import java.util.ArrayDeque;

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

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

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

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

Output:

Head of the ArrayDeque: Complete project report

Handling an Empty ArrayDeque

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

Example

import java.util.ArrayDeque;

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

        // Attempting to retrieve the head of the empty ArrayDeque using peek
        String headTask = tasks.peek();

        // Printing the result
        if (headTask == null) {
            System.out.println("ArrayDeque is empty.");
        } else {
            System.out.println("Head of the ArrayDeque: " + headTask);
        }
    }
}

Output:

ArrayDeque is empty.

Real-World Use Case

Use Case: Task Management System

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

Example

import java.util.ArrayDeque;

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

        // Adding initial tasks to the ArrayDeque
        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 next task using peek
        Task nextTask = tasks.peek();

        // Printing the next task to be processed
        if (nextTask == null) {
            System.out.println("No tasks to process.");
        } else {
            System.out.println("Next task to be processed: " + nextTask);
        }
    }
}

class Task {
    private String description;
    private int priority;

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

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

Output:

Next task to be processed: Complete project report (Priority: 2)

Conclusion

The ArrayDeque.peek() method in Java is used for accessing the head element of a deque without removing it. Understanding how to use this method allows you to safely check the next element to be processed, making it particularly useful in applications like task management systems where you need to verify the next task without altering the deque.

Comments