Last Updated : 11 Jul, 2025
The
removeAll()method in
CopyOnWriteArrayList classthat 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
collectionwhich 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:
Below examples illustrates the removeAll() method:
Example 1: Java
// 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);
}
}
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
Java
// 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);
}
}
}
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4