Java ArrayDeque removeLastOccurrence() Method

The ArrayDeque class in Java provides the removeLastOccurrence() method to remove the last occurrence of a specified element from the deque.

Table of Contents

  1. Introduction
  2. removeLastOccurrence Method Syntax
  3. Examples
    • Removing the Last Occurrence of an Element in the ArrayDeque
    • Handling an Element Not Present in the ArrayDeque
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The ArrayDeque.removeLastOccurrence(Object o) method is used to remove the last occurrence of the specified element from the deque. If the element is not present, the deque remains unchanged.

removeLastOccurrence Method Syntax

The syntax for the removeLastOccurrence method is as follows:

public boolean removeLastOccurrence(Object o)
  • The method takes a single parameter o of type Object, which is the element to be removed from the deque.
  • The method returns a boolean value: true if the deque was modified as a result of the call, false otherwise.

Examples

Removing the Last Occurrence of an Element in the ArrayDeque

The removeLastOccurrence method can be used to remove the last occurrence of a specified element in an ArrayDeque.

Example

import java.util.ArrayDeque;

public class ArrayDequeRemoveLastOccurrenceExample {
    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");
        tasks.add("Email client updates"); // Duplicate element

        // Removing the last occurrence of "Email client updates"
        boolean isRemoved = tasks.removeLastOccurrence("Email client updates");

        // Printing the result of the removeLastOccurrence operation
        System.out.println("Was the element removed? " + isRemoved);

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

Output:

Was the element removed? true
ArrayDeque after removeLastOccurrence: [Complete project report, Email client updates, Prepare presentation]

Handling an Element Not Present in the ArrayDeque

When the specified element is not present in the ArrayDeque, the removeLastOccurrence method returns false.

Example

import java.util.ArrayDeque;

public class ElementNotPresentRemoveLastOccurrenceExample {
    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");

        // Attempting to remove an element not present in the ArrayDeque
        boolean isRemoved = tasks.removeLastOccurrence("Prepare presentation");

        // Printing the result of the removeLastOccurrence operation
        System.out.println("Was the element removed? " + isRemoved);

        // Printing the ArrayDeque after attempting to remove the element
        System.out.println("ArrayDeque after removeLastOccurrence: " + tasks);
    }
}

Output:

Was the element removed? false
ArrayDeque after removeLastOccurrence: [Complete project report, Email client updates]

Real-World Use Case

Use Case: Task Management System

In a task management system, you might need to remove the last occurrence of a specific task from the deque, such as a task that has been completed or canceled. The removeLastOccurrence method can help achieve this functionality.

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));
        tasks.add(new Task("Email client updates", 1)); // Duplicate task

        // Removing the last occurrence of the specified task
        boolean isRemoved = tasks.removeLastOccurrence(new Task("Email client updates", 1));

        // Printing the result of the removeLastOccurrence operation
        System.out.println("Was the task removed? " + isRemoved);

        // 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 boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Task task = (Task) o;

        if (priority != task.priority) return false;
        return description != null ? description.equals(task.description) : task.description == null;
    }

    @Override
    public int hashCode() {
        int result = description != null ? description.hashCode() : 0;
        result = 31 * result + priority;
        return result;
    }

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

Output:

Was the task removed? true
Remaining tasks in ArrayDeque: [Complete project report (Priority: 2), Email client updates (Priority: 1), Prepare presentation (Priority: 3)]

Conclusion

The ArrayDeque.removeLastOccurrence(Object o) method in Java is used for removing the last occurrence of a specified element from a deque. Understanding how to use this method allows you to efficiently manage elements in the deque, making it particularly useful in applications like task management systems where you need to remove specific tasks based on certain criteria.

Comments