Java ArrayDeque retainAll() Method

The ArrayDeque class in Java provides the retainAll() method to retain only the elements in the deque that are contained in the specified collection.

Table of Contents

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

Introduction

The ArrayDeque.retainAll(Collection<?> c) method is used to retain only the elements in the deque that are also contained in the specified collection. This method is useful for filtering the deque to keep only certain elements based on specific criteria.

retainAll Method Syntax

The syntax for the retainAll method is as follows:

public boolean retainAll(Collection<?> c)
  • The method takes a single parameter c of type Collection<?>, which is the collection containing elements to be retained in the deque.
  • The method returns a boolean value: true if the deque was modified as a result of the call, false otherwise.

Examples

Retaining Elements in the ArrayDeque Using retainAll

The retainAll method can be used to retain only the elements in the ArrayDeque that are contained in the specified collection.

Example

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

public class ArrayDequeRetainAllExample {
    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 retained
        Collection<String> tasksToRetain = new ArrayList<>();
        tasksToRetain.add("Email client updates");
        tasksToRetain.add("Prepare presentation");

        // Retaining the specified elements in the ArrayDeque
        boolean isModified = tasks.retainAll(tasksToRetain);

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

        // Printing the ArrayDeque after retaining the elements
        System.out.println("ArrayDeque after retainAll: " + tasks);
    }
}

Output:

Was the ArrayDeque modified? true
ArrayDeque after retainAll: [Email client updates, Prepare presentation]

Handling an Empty Collection

When the specified collection is empty, the retainAll method removes all elements from the deque and returns true.

Example

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

public class EmptyCollectionRetainAllExample {
    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 retained
        Collection<String> tasksToRetain = new ArrayList<>();

        // Retaining the specified elements in the ArrayDeque
        boolean isModified = tasks.retainAll(tasksToRetain);

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

        // Printing the ArrayDeque after retaining the elements
        System.out.println("ArrayDeque after retainAll: " + tasks);
    }
}

Output:

Was the ArrayDeque modified? true
ArrayDeque after retainAll: []

Real-World Use Case

Use Case: Task Management System

In a task management system, you may need to retain only specific tasks in the deque based on certain criteria, such as tasks that are marked as high priority. The retainAll 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 retained
        Collection<Task> tasksToRetain = new ArrayList<>();
        tasksToRetain.add(new Task("Email client updates", 1));
        tasksToRetain.add(new Task("Prepare presentation", 3));

        // Retaining the specified tasks in the ArrayDeque
        boolean isModified = tasks.retainAll(tasksToRetain);

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

        // Printing the ArrayDeque after retaining the tasks
        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 retained? true
Remaining tasks in ArrayDeque: [Email client updates (Priority: 1), Prepare presentation (Priority: 3)]

Conclusion

The ArrayDeque.retainAll(Collection<?> c) method in Java is used for retaining only the elements in a deque that are 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 retain tasks based on specific criteria.

Comments