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 parameter
c which is the collection containing elements to be removed from this set.
Return Value: This method returns a
boolean value such as true, if the CopyOnWriteArraySet is changed. Else this method returns false.
Exceptions: This method throws following Exceptions:
- ClassCastException: if the class of an element of this set is incompatible with the specified collection
- NullPointerException: if this set contains a null element and the specified collection does not permit null elements, or if the specified collection is null.
Below program illustrates the removeAll() function of CopyOnWriteArrayList class:
Program 1:
Output:
CopyOnWriteArraySet: [10, 20, 30, 50]
ArrayList: [10, 30, 40]
Elements removed from CopyOnWriteArraySet: true
Updated CopyOnWriteArraySet: [20, 50]
Program 2: To show NullpointerException