CopyOnWriteArraySet in java

CopyOnWriteArraySet is a Set that uses an internal CopyOnWriteArrayList for all of its operations. It is introduced in JDK 1.5, we can say that it is thread-safe version of Set.

It share some properties of Set and also has its own properties:

  • The internal implementation of CopyOnWriteArraySet is CopyOnWriteArrayList only.
  • Insertion order is preserved and duplicates are allowed.
  • Multiple Threads are able to perform update operation simultaneously but for every update operation a separate cloned copy is created. As for every update a new cloned copy will be created which is costly. Hence if multiple update operation are required then it is not recommended to use CopyOnWriteArraySet.
  • While one thread iterating the Set, other threads can perform updation, here we wont get any runtime exception like ConcurrentModificationException.
  • Iterator of CopyOnWriteArraySet class can perform only read only and wont perform deletion, otherwise we will get Run-time exception UnsupportedOperationException.

Constructors of CopyOnWriteArraySet:

  • CopyOnWriteArraySet c = new CopyOnWriteArraySet(): Creates an empty set.
  • CopyOnWriteArraySet c = new CopyOnWriteArraySet(Collection c): Creates a set containing all of the elements of the specified collection.

NOTE: All methods of CopyOnWriteArraySet is extended from the Collections only because Set interface does not contain any specific methods. CopyOnWriteArraySet does not contain any new methods.

// Java program to illustrate 
// CopyOnWriteArraySet class
import java.util.concurrent.*;
import java.util.*;
class ConcurrentDemo extends Thread {
    static CopyOnWriteArraySet l = new CopyOnWriteArraySet();
    public void run()
        // Child thread trying to add
        // new element in the Set object
    public static void main(String[] args) 
        // We create a child thread 
        // that is going to modify 
        // CopyOnWriteArraySet l.
        ConcurrentDemo t = new ConcurrentDemo();
        // Wait for the thread to 
        // add the element.
        try {
        catch (InterruptedException e) {
            System.out.println("child going to add element");
        // Now we iterate through the 
        // CopyOnWriteArraySet and we
        // wont get exception.
        Iterator itr = l.iterator();
        while (itr.hasNext()) 
            String s = (String);
                // Here we will get
                // RuntimeException


[A, B, C, D]
Exception in thread "main" java.lang.UnsupportedOperationException

My Personal Notes arrow_drop_up

Hello everyone, I am Bishal KUMAR Dubey and most importantly an idea creator. I just love Programming languages and love to know new concepts everyday,every minute,every second. Here to help Other GEEKS!!!

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.