CopyOnWriteArraySet addAll() method in Java with Examples

The addAll() method of CopyonWriteArraySet method adds all the element of the specified collection to this CopyOnWriteArraySet which are not present in it. This methods results the union of the two collections.

Syntax:

public boolean addAll(Collection<E> c)

Parameters: This method accepts a parameter c which is the collection containing elements to be added to 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 NullPointerException if the specified collection is null.

Below program illustrates the addAll() function of CopyOnWriteArrayList class:

Program 1: In the below program, the specified collection elements are added to the CopyOnWriteArraySet. Since 50 is common in both the collections, it is added single time.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the
// CopyOnWriteArraySet addall() 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(50);
        Arrlist.add(60);
        Arrlist.add(70);
        Arrlist.add(80);
  
        // print ArrayList
        System.out.println("ArrayList:  "
                           + Arrlist);
  
        // adding all elements of Arraylist
        // in the CopyOnWriteArraySet
        ArrSet.addAll(Arrlist);
  
        // print updated CopyOnWriteArraySet
        System.out.println("Updated CopyOnWriteArraySet: "
                           + ArrSet);
    }
}

chevron_right


Output:

CopyOnWriteArraySet: [10, 20, 30, 50]
ArrayList:  [50, 60, 70, 80]
Updated CopyOnWriteArraySet: [10, 20, 30, 50, 60, 70, 80]

Program 2: To show NullpointerException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate the
// CopyOnWriteArraySet addall() 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>();
  
        try {
  
            // adding null to the CopyOnWriteArraySet
            // This will throw NullPointerException
            ArrSet.addAll(null);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : ManasChhabra2