Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList removeAll() Method

Posted on July 24, 2025July 24, 2025 By Admin No Comments on Java ArrayList removeAll() Method

The removeAll() method is an essential operation in the Java ArrayList class that allows you to remove all elements from a list that are contained in a specified collection. It streamlines the process of deleting multiple entries at once, making it powerful for list management, filtering, or bulk removal tasks.

What Is the removeAll() Method in ArrayList?

  • The removeAll() method removes every occurrence of the elements in the specified collection from the calling ArrayList. Unlike remove(), which deletes only one item at a time, removeAll() can handle multiple items in a single operation.
  • This method is particularly useful for removing duplicates, subtracting one list from another, or clearing a list by passing itself as an argument.

Syntax

public boolean removeAll(Collection<?> c)

Parameters

ParameterDescription
cThe collection containing elements to be removed from the list.

Return Value

  • Returns true if the list was changed as a result of the call.
  • Returns false if no elements were removed.

Exceptions

  • NullPointerException – If the specified collection is null.
  • ClassCastException – If an element of the list is incompatible with the elements in the collection (rare; occurs only if classes don’t match).

How Does removeAll() Work Internally?

  • The method loops through the list and checks each element for membership in the specified collection using contains().
  • If found, the element is removed (all occurrences are processed).
  • The process continues until all elements in the specified collection are checked and removed where found.
  • The operation is done in-place, modifying the original list.

Examples of the removeAll() Method

1. Remove All Elements from an ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> cars = new ArrayList<>();
        cars.add("Volvo");
        cars.add("BMW");
        cars.add("Ford");

        // Remove all elements by passing itself
        cars.removeAll(cars);
        System.out.println(cars); // Output: []
    }
}

This removes every item from the list, resulting in an empty list.

2. Remove All Elements Matching Another Collection

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        colors.add("Green");

        ArrayList<String> remove = new ArrayList<>();
        remove.add("Green");
        remove.add("Yellow");

        colors.removeAll(remove);

        System.out.println(colors); // Output: [Red, Blue]
    }
}

Here, both occurrences of “Green” are removed from colors, but “Yellow” is ignored since it doesn’t exist.

3. Remove All Elements from the Beginning, Middle, or End

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10); // Index 0
        numbers.add(20); // Index 1
        numbers.add(30); // Index 2

        ArrayList<Integer> toRemove = new ArrayList<>();
        toRemove.add(10);
        toRemove.add(30);

        numbers.removeAll(toRemove);

        System.out.println(numbers); // Output: [20]
    }
}

Now only element 20 remains.

4. Remove with Null or Incompatible Collections

import java.util.ArrayList;

public class ErrorExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("A");

        // Example: Passing null will throw NullPointerException
        // list.removeAll(null);

        // Use try-catch to handle exception
        try {
            list.removeAll(null);
        } catch (NullPointerException e) {
            System.out.println("Cannot pass null as collection: " + e.getMessage());
        }
    }
}

Passing null causes a NullPointerException.

Important Notes

  • Only elements present in both the list and the specified collection are removed (all their occurrences).
  • Order is preserved for the remaining elements.
  • The method modifies the original list directly (it is not a copy).
  • If the collection passed is empty, the method does nothing and returns false.
  • Works with all types: primitives (as wrappers), Strings, and custom objects (based on equals() method).

Summary

AspectDetails
Method PurposeRemove all elements from an ArrayList that exist in a given collection
Syntaxlist.removeAll(Collection<?> c)
Return Valuetrue if any elements removed, false otherwise
ParametersA collection of elements to remove
ExceptionsNullPointerException, ClassCastException
Typical Use CasesBulk removal, subtracting one list from another, batch filtering

Typical Use Cases

  • Bulk deletion (remove many elements at once).
  • List difference (subtract one list from another).
  • Cleaning up processed or excluded data.
  • Filtering out unwanted items from one list based on another.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList remove() Method
Next Post: Java ArrayList removeIf() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme