HomeLanguagesJavaCopyOnWriteArraySet removeAll() method in Java with Examples

CopyOnWriteArraySet removeAll() method in Java with Examples

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:




// Java Program to illustrate the CopyOnWriteArraySet
// removeall() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<Integer> ArrSet
            = new CopyOnWriteArraySet<Integer>();
  
        // Add elements
        ArrSet.add(10);
        ArrSet.add(20);
        ArrSet.add(30);
        ArrSet.add(50);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        // create object of ArrayList
        ArrayList<Integer> Arrlist
            = new ArrayList<Integer>();
        // add elements
        Arrlist.add(10);
        Arrlist.add(30);
        Arrlist.add(40);
  
        // print ArrayList
        System.out.println("ArrayList: " + Arrlist);
  
        // removing the common elements from
        // the CopyOnWriteArraySet using removeAll()
        System.out.println("Elements removed "
                           + "from CopyOnWriteArraySet: "
                           + ArrSet.removeAll(Arrlist));
  
        // print updated CopyOnWriteArraySet
        System.out.println("Updated CopyOnWriteArraySet: "
                           + ArrSet);
    }
}


Output:

CopyOnWriteArraySet: [10, 20, 30, 50]
ArrayList: [10, 30, 40]
Elements removed from CopyOnWriteArraySet: true
Updated CopyOnWriteArraySet: [20, 50]

Program 2: To show NullpointerException




// Java Program to illustrate the CopyOnWriteArraySet
// removeall() method in Java
  
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of CopyOnWriteArraySet
        CopyOnWriteArraySet<Integer> ArrSet
            = new CopyOnWriteArraySet<Integer>();
  
        // Add elements
        ArrSet.add(10);
        ArrSet.add(20);
        ArrSet.add(30);
        ArrSet.add(50);
  
        // print CopyOnWriteArraySet
        System.out.println("CopyOnWriteArraySet: "
                           + ArrSet);
  
        try {
  
            // removing the null from
            // the CopyOnWriteArraySet using removeAll()
            // This will throw NullPointerException
  
            System.out.println("Elements removed "
                               + "from CopyOnWriteArraySet: "
                               + ArrSet.removeAll(null));
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

CopyOnWriteArraySet: [10, 20, 30, 50]
java.lang.NullPointerException
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS