VOOZH about

URL: https://www.geeksforgeeks.org/java/copyonwritearrayset-addall-method-in-java-with-examples/

⇱ CopyOnWriteArraySet addAll() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CopyOnWriteArraySet addAll() method in Java with Examples

Last Updated : 11 Jul, 2025
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.
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
Output:
CopyOnWriteArraySet: [10, 20, 30, 50]
java.lang.NullPointerException
Comment