CopyOnWriteArrayList removeAll() method in Java with Examples

The removeAll() method in CopyOnWriteArrayList class that removes all the elements that are contained in the specified collection from the CopyOnArrayList object you call on.

Syntax:

public boolean removeAll(Collection collection)

Parameter: The method accepts only a single parameter collection which is to be removed from the calling object.



Return Value: This method returns a boolean value. It returns true if this remove operation is successful.

Exception: This method throws following exceptions:

  • ClassCastException: if the class of an element of this list is incompatible with the specified collection.
  • NullPointerException: if the specified collection is null or if this list contains a null element and the specified collection does not permit null elements.

Below examples illustrates the removeAll() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate removeAll() method
  
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
  
public class Demo {
  
    public static void main(String args[])
    {
  
        // Get the CopyOnWriteArrayList
        CopyOnWriteArrayList<String> wishlist
            = new CopyOnWriteArrayList<>();
  
        // Add the elements in the CopyOnWriteArrayList
        wishlist.add("TV");
        wishlist.add("computer");
        wishlist.add("play station");
        wishlist.add("mobile");
        wishlist.add("smart watch");
  
        // Print the CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: \n"
                           + wishlist);
  
        // Get the collection to be removed
        ArrayList<String> checkList
            = new ArrayList<>();
  
        checkList.add("play station");
        checkList.add("TV");
        checkList.add("mobile");
  
        System.out.println("\nCollection to be removed:\n"
                           + checkList);
  
        // Remove the collection from CopyOnWriteArrayList
        // using removeAll() method
        wishlist.removeAll(checkList);
  
        // Print the CopyOnWriteArrayList after removal
        System.out.println("\nAfter removal of collection"
                           + " from CopyOnWriteArrayList:\n"
                           + wishlist);
    }
}

chevron_right


Output:

CopyOnWriteArrayList: 
[TV, computer, play station, mobile, smart watch]

Collection to be removed:
[play station, TV, mobile]

After removal of collection from CopyOnWriteArrayList:
[computer, smart watch]

Example 2: To show NullPointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate removeAll() method
  
import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
  
public class Demo {
  
    public static void main(String args[])
    {
  
        // Get the CopyOnWriteArrayList
        CopyOnWriteArrayList<String> wishlist
            = new CopyOnWriteArrayList<>();
  
        // Add the elements in the CopyOnWriteArrayList
        wishlist.add("TV");
        wishlist.add("computer");
        wishlist.add("play station");
        wishlist.add("mobile");
        wishlist.add("smart watch");
  
        // Print the CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList: \n"
                           + wishlist);
  
        // Get the collection to be removed
        ArrayList<String> checkList
            = null;
        System.out.println("\nCollection to be removed: "
                           + checkList);
  
        try {
  
            // Remove the collection from CopyOnWriteArrayList
            // using removeAll() method
            wishlist.removeAll(checkList);
        }
        catch (Exception e) {
  
            // Print the Exception
            System.out.println("\nException thrown"
                               + " while removing null "
                               + "from the CopyOnWriteArrayList: \n"
                               + e);
        }
    }
}

chevron_right


Output:

CopyOnWriteArrayList: 
[TV, computer, play station, mobile, smart watch]

Collection to be removed: null

Exception thrown while removing null from the CopyOnWriteArrayList: 
java.lang.NullPointerException


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.