The removeAll() method of the ArrayList class removes all elements from an ArrayList that are also present in a specified collection. It compares each element of the list with the elements of the given collection and removes every matching element. The method returns true if the list is modified; otherwise, it returns false.
- Does not affect elements that are not present in the given collection.
- Throws NullPointerException if the specified collection is null.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// create an ArrayList of fruits
ArrayList<String> l = new ArrayList<>();
l.add("Cherry");
l.add("Blueberry");
l.add("Apple");
l.add("Grapes");
// Removing all elements
// from the ArrayList
l.removeAll(l);
// Printing the ArrayList after removal
System.out.println("" + l);
}
}
Output
[]
Explanation: In this example, the same ArrayList is passed as the argument to removeAll(). Since every element of the list is also present in the specified collection, all elements are removed, leaving the list empty.
Syntax
public boolean removeAll(Collection<?> c)
- Parameters: This method takes "Collection c" as a parameter containing elements to be removed from this list.
- Returns Value: This method returns true if the list was modified as a result of the operation, otherwise false.
- Exception: This method throws NullPointerException if the list contains a null element and the specified collection does not permit null elements (optional), or if the specified collection is null.
Example: Removing Specific Elements Using Another Collection
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// Creating an ArrayList of numbers
ArrayList<Integer> n1 = new ArrayList<>();
n1.add(1);
n1.add(2);
n1.add(3);
n1.add(4);
n1.add(5);
System.out.println("Original list: " + n1);
// Creating another ArrayList
// with elements to remove
ArrayList<Integer> n2 = new ArrayList<>();
n2.add(1);
n2.add(2);
n2.add(3);
// Removing specified elements
// using removeAll()
n1.removeAll(n2);
System.out.println("List after removing specific elements: " + n1);
}
}
Output
Original list: [1, 2, 3, 4, 5] List after removing specific elements: [4, 5]
Explanation: In this example, two ArrayList<Integer> objects are created. The removeAll() method removes every element from the first list that is also present in the second list. Elements that are not found in the specified collection remain unchanged.
Example: Using removeAll() with a Null Collection
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
try {
// Creating an ArrayList of numbers
ArrayList<Integer> n = new ArrayList<>();
n.add(1);
n.add(2);
n.add(3);
n.add(4);
n.add(5);
System.out.println("Original list: " + n);
// Defining a null collection
ArrayList<Integer> n1 = null;
// Trying to remove elements
// using removeAll()
System.out.println("\nTrying to use removeAll() with a null list.");
n.removeAll(n1);
} catch (NullPointerException e) {
System.out.println("Exception thrown: " + e);
}
}
}
Output
Original list: [1, 2, 3, 4, 5] Trying to use removeAll() with a null list. Exception thrown: java.lang.NullPointerException
Explanation: Here, a null collection is passed to removeAll(). Since the method expects a valid collection for comparison, it throws a NullPointerException.
Advantages of removeAll()
- Removes multiple elements in a single method call.
- Reduces the need for manual iteration.
- Automatically removes all matching occurrences.
- Returns whether the list was modified.
- Works with any collection implementing the Collection interface.
- Simplifies bulk removal operations.