Java ArrayDeque getLast() Method

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

Table of Contents

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

Introduction

The ArrayDeque.getLast() method is used to retrieve, but not remove, the last element of the ArrayDeque. If the deque is empty, the method throws a NoSuchElementException.

getLast Method Syntax

The syntax for the getLast method is as follows:

public E getLast()
  • The method does not take any parameters.
  • The method returns the last element of the deque.

Examples

Retrieving the Last Element of the ArrayDeque

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

Example

import java.util.ArrayDeque;

public class ArrayDequeGetLastExample {
    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 getLast
        String lastTask = tasks.getLast();

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

Output:

Last task: Prepare presentation

Handling an Empty ArrayDeque

When the ArrayDeque is empty, the getLast method throws a NoSuchElementException.

Example

import java.util.ArrayDeque;
import java.util.NoSuchElementException;

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

        try {
            // Attempting to retrieve the last element of the empty ArrayDeque using getLast
            String lastTask = tasks.getLast();
            System.out.println("Last task: " + lastTask);
        } catch (NoSuchElementException e) {
            System.out.println("ArrayDeque is empty: " + e.getMessage());
        }
    }
}

Output:

ArrayDeque is empty: null

Real-World Use Case

Use Case: Task Management System

In a task management system, you might need to check the most recently added task without removing it from the deque. The getLast method can be used to view the last task to be processed or reviewed.

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 getLast
        Task lastTask = tasks.getLast();

        // Printing the last task to be processed or reviewed
        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.getLast() method in Java is used for accessing the last element of a deque without removing it. Understanding how to use this method allows you to effectively manage and view the most recently added elements in the deque, making it particularly useful in applications like task management systems where you need to check the last task to be processed or reviewed without altering the deque.

Comments