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:
// 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); } } |
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 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); } } } |
CopyOnWriteArrayList: [TV, computer, play station, mobile, smart watch] Collection to be removed: null Exception thrown while removing null from the CopyOnWriteArrayList: java.lang.NullPointerException
Recommended Posts:
- Set removeAll() method in Java with Examples
- CopyOnWriteArraySet removeAll() method in Java with Examples
- SortedSet removeAll() method in Java with Examples
- LinkedBlockingDeque removeAll() method in Java with Examples
- List removeAll() method in Java with Examples
- ArrayList removeAll() method in Java with Examples
- LinkedTransferQueue removeAll() method in Java with Examples
- AbstractSet removeAll() Method in Java with Examples
- CopyOnWriteArrayList set() method in Java with Examples
- CopyOnWriteArrayList retainAll() method in Java with Examples
- CopyOnWriteArrayList removeIf() method in Java with Examples
- CopyOnWriteArrayList remove() method in Java with Examples
- CopyOnWriteArrayList addAllAbsent() method in Java with Examples
- CopyOnWriteArrayList subList() method in Java with Examples
- CopyOnWriteArrayList forEach() method in Java with Examples
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.



