Java ArrayDeque removeAll() Method

The ArrayDeque class in Java provides the removeAll() method to remove all elements in the deque that are also contained in the specified collection.

Table of Contents

  1. Introduction
  2. removeAll Method Syntax
  3. Examples
    • Removing Elements from the ArrayDeque Using removeAll
    • Handling an Empty Collection
  4. Real-World Use Case
    • Use Case: Task Management System
  5. Conclusion

Introduction

The ArrayDeque.removeAll(Collection<?> c) method is used to remove all elements from the deque that are also contained in the specified collection. This method can be useful when you need to filter out specific elements from the deque.

removeAll Method Syntax

The syntax for the removeAll method is as follows:

public boolean removeAll(Collection<?> c)
  • The method takes a single parameter c of type Collection<?>, which is the collection containing elements 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 Elements from the ArrayDeque Using removeAll

The removeAll method can be used to remove all elements from the ArrayDeque that are contained in the specified collection.

Example

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;

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

        // Creating a collection of tasks to be removed
        Collection<String> tasksToRemove = new ArrayList<>();
        tasksToRemove.add("Email client updates");
        tasksToRemove.add("Prepare presentation");

        // Removing the specified elements from the ArrayDeque
        boolean isModified = tasks.removeAll(tasksToRemove);

        // Printing the result of the removeAll operation
        System.out.println("Was the ArrayDeque modified? " + isModified);

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

Output:

Was the ArrayDeque modified? true
ArrayDeque after removeAll: [Complete project report]

Handling an Empty Collection

When the specified collection is empty, the removeAll method returns false and the deque remains unchanged.

Example

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;

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

        // Creating an empty collection of tasks to be removed
        Collection<String> tasksToRemove = new ArrayList<>();

        // Attempting to remove the specified elements from the ArrayDeque
        boolean isModified = tasks.removeAll(tasksToRemove);

        // Printing the result of the removeAll operation
        System.out.println("Was the ArrayDeque modified? " + isModified);

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

Output:

Was the ArrayDeque modified? false
ArrayDeque after removeAll: [Complete project report, Email client updates]

Real-World Use Case

Use Case: Task Management System

In a task management system, you may need to remove multiple tasks from the deque based on a specific criteria, such as tasks that have been completed. The removeAll method can help achieve this functionality.

Example

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;

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("Team meeting", 4));

        // Creating a collection of tasks to be removed
        Collection<Task> tasksToRemove = new ArrayList<>();
        tasksToRemove.add(new Task("Email client updates", 1));
        tasksToRemove.add(new Task("Prepare presentation", 3));

        // Removing the specified tasks from the ArrayDeque
        boolean isModified = tasks.removeAll(tasksToRemove);

        // Printing the result of the removeAll operation
        System.out.println("Were the tasks removed? " + isModified);

        // 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:

Were the tasks removed? true
Remaining tasks in ArrayDeque: [Complete project report (Priority: 2), Team meeting (Priority: 4)]

Conclusion

The ArrayDeque.removeAll(Collection<?> c) method in Java is used for removing all elements from a deque that are also contained in a specified collection. Understanding how to use this method allows you to efficiently manage and filter elements in the deque, making it particularly useful in applications like task management systems where you need to remove multiple tasks based on specific criteria.

Comments