Java ArrayList removeAll() Method

The ArrayList.removeAll(Collection<?> c) method in Java is used to remove all elements in the specified collection from the ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. removeAll Method Syntax
  3. Examples
    • Removing Elements in a Collection
    • Handling Elements Not Present in the ArrayList
    • Handling Empty Collections
  4. Conclusion

Introduction

The ArrayList.removeAll(Collection<?> c) method is a member of the ArrayList class in Java. It allows you to remove all elements in the specified collection from the ArrayList. This method is particularly useful for bulk removal of elements.

removeAll Method Syntax

The syntax for the removeAll method is as follows:

public boolean removeAll(Collection<?> c)
  • c: The collection containing elements to be removed from the ArrayList.

The method returns true if the ArrayList was modified as a result of the operation, and false otherwise.

Examples

Removing Elements in a Collection

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

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoveAllExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange", "Grapes"));
        List<String> elementsToRemove = Arrays.asList("Banana", "Grapes");

        System.out.println("ArrayList before removal: " + list);

        // Remove all elements in the specified collection
        boolean removed = list.removeAll(elementsToRemove);

        System.out.println("Was the ArrayList modified? " + removed);
        System.out.println("ArrayList after removal: " + list);
    }
}

Output:

ArrayList before removal: [Apple, Banana, Orange, Grapes]
Was the ArrayList modified? true
ArrayList after removal: [Apple, Orange]

Handling Elements Not Present in the ArrayList

If none of the elements in the specified collection are present in the ArrayList, the removeAll method returns false.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoveAllNotPresentExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));
        List<String> elementsToRemove = Arrays.asList("Grapes", "Pineapple");

        // Attempt to remove elements not present in the list
        boolean removed = list.removeAll(elementsToRemove);

        System.out.println("Was the ArrayList modified? " + removed);
        System.out.println("ArrayList after attempting to remove elements: " + list);
    }
}

Output:

Was the ArrayList modified? false
ArrayList after attempting to remove elements: [Apple, Banana, Orange]

Handling Empty Collections

If the specified collection is empty, the removeAll method returns false and the ArrayList remains unchanged.

Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RemoveAllEmptyCollectionExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Orange"));
        List<String> elementsToRemove = Arrays.asList();

        // Attempt to remove elements from an empty collection
        boolean removed = list.removeAll(elementsToRemove);

        System.out.println("Was the ArrayList modified? " + removed);
        System.out.println("ArrayList after attempting to remove elements: " + list);
    }
}

Output:

Was the ArrayList modified? false
ArrayList after attempting to remove elements: [Apple, Banana, Orange]

Conclusion

The ArrayList.removeAll(Collection<?> c) method in Java is used for removing all elements in a specified collection from an ArrayList. By understanding how to use this method, you can efficiently manage the contents of your lists in Java applications. Whether you are removing elements that are present, handling elements not present, or dealing with empty collections, the removeAll method provides a reliable solution for these tasks.

Comments