ArrayList removeAll() Method in Java with Examples
The removeAll() method of the ArrayList class in Java is used to remove all elements of an ArrayList
that are specified in another collection or within the same list itself.
Example 1: Here, we use the removeAll() method to remove all elements from an ArrayList by passing the same list as an argument.
// Java program to demonstrate removeAll()
// by removing all elements from the same ArrayList
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
[]
Syntax of ArrayList removeAll() Method
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 2: Here, we use removeAll() method to remove specific elements from an ArrayList using another list.
// Java program to demonstrate removeAll() method
// // by removing specific elements using another list
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 the above example, we create two ArrayList of Integers one for storing all the elements and another containing elements to be removed. Then the removeAll() method removes only the specified elements.
Example 3: Here, we use the removeAll() method to demonstrate how passing a null collection as an argument results in a NullPointerException.
// Java program to demonstrate
// NullPointerException in removeAll()
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