Open In App

CopyOnWriteArraySet addAll() method in Java with Examples

Last Updated : 10 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.




// 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);
    }
}


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




// 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);
        }
    }
}


Output:

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


Similar Reads

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&lt;E&gt; c) Parameters: This method accepts a para
2 min read
CopyOnWriteArraySet removeIf() method in Java with Examples
The removeIf() method of CopyonWriteArraySet method removes the element from this CopyOnWriteArraySet that satisfies the specified condition. Syntax: public boolean removeIf (Predicate&lt;E&gt; filter) Parameters: This method accepts a mandatory parameter filter which is the predicate value based on which elements are removed from this set. Return
2 min read
CopyOnWriteArraySet forEach() method in Java with Examples
The forEach() method of CopyOnWriteArraySet is an in-built function in Java which is used to traverse each element in this Set. Syntax: public void forEach (Consumer&lt;E&gt; action) Parameters: This method takes a parameter action which represents the action to be performed for each element. Return Value: This method does not returns anything. Exc
2 min read
Collections addAll() method in Java with Examples
The addAll() method of java.util.Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under
3 min read
AbstractList addAll() method in Java with Examples
The addAll() method of java.util.AbstractList class is used to insert all of the elements in the specified collection into this list at the specified position. This shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that th
4 min read
AbstractQueue addAll() method in Java with examples
The addAll(E e) method of AbstractQueue adds all of the elements in the specified collection to this queue. Syntax: public boolean addAll(Collection c) Parameters: This method accepts a mandatory parameter collection containing elements to be added to this queue Returns: The method returns true if this queue changed as a result of the call Exceptio
3 min read
AbstractCollection addAll() Method in Java with Examples
The addAll(Collection C) method of Java AbstractCollection is used to append all of the elements from a mentioned collection to this collection. The elements are added randomly without following any specific order. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of any type that is to be added to the collection. Ret
2 min read
AbstractSequentialList addAll() Method in Java with Examples
The addAll(int index, Collection C) method of AbstractSequentialList is used to append all of the elements from the collection passed as a parameter to this function at a specific index or position of a abstract sequential list. Syntax: boolean addAll(int index, Collection C) Parameters: This function accepts two parameters as shown in the above sy
2 min read
Collection addAll() method in Java with Examples
The addAll(Collection collection) of java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false. Syntax: Collection.addAll(Collection&lt;E&gt; collection)
3 min read
ConcurrentLinkedDeque addAll() method in Java with Examples
The addAll(Collection col) of ConcurrentLinkedDeque which takes col as a parameter, where col is a Collection of elements (List, ArrayList, LinkedList etc). This entire Collection gets appended or added to the end of the Dequeue. This method just like add() method returns true if the Collection gets appended to the Deque. Syntax: public boolean add
3 min read