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.
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:
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
CopyOnWriteArrayList: [TV, computer, play station, mobile, smart watch] Collection to be removed: null Exception thrown while removing null from the CopyOnWriteArrayList: java.lang.NullPointerException
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.