Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
CopyOnWriteArraySet in Java
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
import java.util.concurrent.*;
public class Demo extends Thread {
public static void main(String[] args) {
CopyOnWriteArraySet cowArraySet = new CopyOnWriteArraySet();
cowArraySet.add("Amy");
cowArraySet.add("John");
cowArraySet.add("Bob");
cowArraySet.add("Clara");
cowArraySet.add("Peter");
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();
cowArraySet.add("Amy");
cowArraySet.add("John");
cowArraySet.add("Bob");
cowArraySet.add("Clara");
cowArraySet.add("Peter");
System.out.println(cowArraySet);Advertisements