The Wayback Machine - https://web.archive.org/web/20250305191022/https://www.geeksforgeeks.org/arraylist-removeall-method-in-java-with-examples/
Open In App

ArrayList removeAll() Method in Java with Examples

Last Updated : 11 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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
// 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
// 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
// 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

Kickstart your Java journey with our online course on Java Programming, covering everything from basics to advanced concepts. Complete real-world coding challenges and gain hands-on experience. Join the Three 90 Challenge—finish 90% in 90 days for a 90% refund. Start mastering Java today!


Next Article
Practice Tags :

Similar Reads

What is the difference between ArrayList.clear() and ArrayList.removeAll()?
In Java, ArrayList.clear() and ArrayList.removeAll() are the two methods that are used to remove elements from an ArrayList, but they are used for different purposes. In this article will explain the difference between the two methods.1. ArrayList.clear() MethodThe clear() method of ArrayList in Java is used to remove all the elements from a list.S
3 min read
SortedSet removeAll() method in Java with Examples
The removeAll() method of SortedSet interface is used to remove from this SortedSet all of its elements that are contained in the specified collection.Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from this SortedSet.Returns Value: This method returns true
3 min read
Java Collection removeAll() Method with Examples
The removeAll() method of Java Collection removes those elements from the collection that are contained in the collection which is given as an argument to function. Syntaxboolean removeAll(Collection<?> c);Parameters:c is the collection containing elements that are removed from the calling collection.Return Type: It returns a boolean value. I
2 min read
List removeAll() method in Java with Examples
This method is used to remove all the elements present in the collection from the specified list. Syntax: boolean removeAll(Collection c) Parameters: This method has only argument, collection of which elements are to be removed from the given list. Returns: This method returns True if elements are removed and list changes. Below programs show the i
2 min read
CopyOnWriteArraySet removeAll() method in Java with Examples
The removeAll() method of CopyonWriteArraySet method removes all the elements of this CopyOnWriteArraySet that are present in the specified collection. That means elements which are common in both the collections are removed from this CopyOnWriteArraySet. Syntax: public boolean removeAll(Collection<E> c) Parameters: This method accepts a para
2 min read
LinkedTransferQueue removeAll() method in Java with Examples
The removeAll() method of java.util.concurrent.LinkedTransferQueue is an in-built function is Java which is used to remove from this queue all of its elements that are contained in the specified collection. Syntax: public boolean removeAll(Collection c) Parameters: This method takes collection c as a parameter containing elements to be removed from
3 min read
LinkedBlockingDeque removeAll() method in Java with Examples
The removeAll() method of LinkedBlockingDeque is an in-built function is Java which is used to remove from this deque all of its elements that are contained in the specified collection. That means, all the common elements these two collections are removed from this deque.Syntax: public boolean removeAll(Collection c) Parameters: This method takes
2 min read
Vector removeAll() Method in Java
The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified. Syntax: Vector.removeAll(Collection col) Parameters: This method accepts a mandatory parameter col which is the collection whose elements are to be removed from the vector. Return Value: This method return
2 min read
ConcurrentSkipListSet removeAll() method in Java
The removeAll() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java which returns removes from this set all of its elements that are contained in the specified collection. If the specified collection is also a set, this operation effectively modifies this set so that its value is the asymmetric set difference of the
3 min read
AbstractSequentialList removeAll() method in Java with Example
The removeAll() method of java.util.AbstractSequentialList class is used to remove from this list all of its elements that are contained in the specified collection. 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 re
3 min read