# CopyOnWriteArraySet in Java

Java 8Object Oriented ProgrammingProgramming

A thread safe version of the set is the CopyOnWriteArraySet in Java. This set uses an CopyOnWriteArrayList internally for the set operations. The CopyOnWriteArraySet was introduced by the JDK 1.5.

A program that demonstrates this is given as follows −

## Example

Live Demo

import java.util.concurrent.*;
public class Demo extends Thread {
public static void main(String[] args) {
CopyOnWriteArraySet cowArraySet = new CopyOnWriteArraySet();
System.out.println(cowArraySet);
}
}

The output of the above program is as follows −

## Output

[Amy, John, Bob, Clara, Peter]

Now let us understand the above program.

The CopyOnWriteArraySet is created and then elements are added to it. Then the elements are displayed. A code snippet that demonstrates this is given as follows −

CopyOnWriteArraySet cowArraySet = new CopyOnWriteArraySet();
System.out.println(cowArraySet);