Java ArrayDeque pollLast() Method

The ArrayDeque class in Java provides the pollLast() method to retrieve and remove the last element of the deque.

Table of Contents

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

Introduction

The ArrayDeque.pollLast() method is used to retrieve and remove the last element of the ArrayDeque. If the deque is empty, the method returns null instead of throwing an exception, making it a safe way to remove the last element.

pollLast Method Syntax

The syntax for the pollLast method is as follows:

public E pollLast()
  • 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 and Removing the Last Element of the ArrayDeque Using pollLast

The pollLast method can be used to retrieve and remove the last element of an ArrayDeque.

Example

import java.util.ArrayDeque;

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

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

        // Printing the ArrayDeque after removal
        System.out.println("ArrayDeque after pollLast: " + tasks);
    }
}

Output:

Last element removed from the ArrayDeque: Prepare presentation
ArrayDeque after pollLast: [Complete project report, Email client updates]

Handling an Empty ArrayDeque

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

Example

import java.util.ArrayDeque;

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

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

        // Printing the result
        if (lastTask == null) {
            System.out.println("ArrayDeque is empty.");
        } else {
            System.out.println("Last element removed from 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 and remove the most recently added task from the deque. The pollLast method can be used to retrieve and remove 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 and removing the last task using pollLast
        Task lastTask = tasks.pollLast();

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

        // Printing the ArrayDeque after removal
        System.out.println("Remaining tasks in ArrayDeque: " + tasks);
    }
}

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:

Processing last task: Prepare presentation (Priority: 3)
Remaining tasks in ArrayDeque: [Complete project report (Priority: 2), Email client updates (Priority: 1)]

Conclusion

The ArrayDeque.pollLast() method in Java is used for retrieving and removing the last element of a deque. Understanding how to use this method allows you to safely and efficiently manage elements in the deque, making it particularly useful in applications like task management systems where you need to process tasks and remove them from the deque as they are completed.

Comments