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
. Unlikeremove()
, 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
Parameter | Description |
---|---|
c | The 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 isnull
.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
Aspect | Details |
---|---|
Method Purpose | Remove all elements from an ArrayList that exist in a given collection |
Syntax | list.removeAll(Collection<?> c) |
Return Value | true if any elements removed, false otherwise |
Parameters | A collection of elements to remove |
Exceptions | NullPointerException , ClassCastException |
Typical Use Cases | Bulk 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.