VOOZH about

URL: https://www.geeksforgeeks.org/java/copyonwritearrayset-add-method-in-java/

⇱ CopyOnWriteArraySet add() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CopyOnWriteArraySet add() method in Java

Last Updated : 11 Jul, 2025
The add(E e) method of CopyOnWriteArraySet inserts the element passed in the parameter to the end of the Set or at a specified index in the Set. The function returns true on addition of new element to the Set. Syntax:
public boolean add(E e)
Parameters: The function accepts a single mandatory parameter element which specifies the element to be added in the Set. Return Value: The function returns true on addition in the Set. Below programs illustrate the above function: Program 1:
Output:
CopyOnWriteArraySet: [2, 3, 4, 7]
On adding 4 it returns false
Updated CopyOnWriteArraySet: [2, 3, 4, 7]
Program 2:
Output:
CopyOnWriteArraySet: [2, 3]
On adding 45 it returns true
Updated CopyOnWriteArraySet: [2, 3, 45]
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArraySet.html#add-E-
Comment