Java ArrayDeque peekLast() Method

The ArrayDeque class in Java provides the peekLast() method to retrieve, but not remove, the last element of the deque.

Table of Contents

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

Introduction

The ArrayDeque.peekLast() method is used to retrieve, but not remove, the last element of the deque. If the deque is empty, the method returns null, making it a safe way to access the last element without modifying the deque.

peekLast Method Syntax

The syntax for the peekLast method is as follows:

public E peekLast()
  • The method does not take any parameters.
  • The method returns the last element of the deque, or null if the deque is empty.

Examples

Retrieving the Last Element of the ArrayDeque Using peekLast

The peekLast method can be used to view the last element of an ArrayDeque.

Example

import java.util.ArrayDeque;

public class ArrayDequePeekLastExample {
    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 last element of the ArrayDeque using peekLast
        String lastTask = tasks.peekLast();

        // Printing the last element of the ArrayDeque
        System.out.println("Last element of the ArrayDeque: " + lastTask);
    }
}

Output:

Last element of the ArrayDeque: Prepare presentation

Handling an Empty ArrayDeque

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

Example

import java.util.ArrayDeque;

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

        // Attempting to retrieve the last element of the empty ArrayDeque using peekLast
        String lastTask = tasks.peekLast();

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

Output:

ArrayDeque is empty.

Real-World Use Case

Use Case: Task Management System

In a task management system, you might need to check the most recently added task at the end of the deque without removing it. The peekLast method can be used to view this task.

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 last task using peekLast
        Task lastTask = tasks.peekLast();

        // Printing the last task to be processed or reviewed
        if (lastTask == null) {
            System.out.println("No tasks to process.");
        } else {
            System.out.println("Last task to be reviewed: " + lastTask);
        }
    }
}

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:

Last task to be reviewed: Prepare presentation (Priority: 3)

Conclusion

The ArrayDeque.peekLast() method in Java is used for accessing the last element of a deque without removing it. This method allows you to safely check the last element, making it particularly useful in applications like task management systems where you need to verify the most recently added task to be processed or reviewed without altering the deque. Understanding how to use peekLast() ensures you can handle deque operations efficiently and safely.

Comments