The CopyOnWriteArraySet class is part of the concurrent collection and it was introduced in JDK 1.5. Some developer thinks ConcurrentHashSet in java but it’s not because there is nothing like in java ConcurrentHashSet. The CopyOnWriteArraySet is thread-safe and it internally uses the CopyOnWriteArrayList for all of its operations. If you haven’t read CopyOnWriteArrayList yet, then you should read it first. As we know HashSet is not threaded safe, So, it is the thread-safe variant of HashSet. In this post, we will discuss what is CopyOnWriteArraySet in java and when we should use it. Let’s see java copyonwritearrayset with example.
Here is the table content of the article will we will cover this topic.
1. What is CopyOnWriteArraySet in Java?
2. Important properties about CopyOnWriteArraySet?
3. How to create CopyOnWriteArraySet?
4. Constructors of CopyOnWriteArraySet?
5. Method of CopyOnWriteArraySet?
6. java copyonwritearrayset example?
1. What is CopyOnWriteArraySet in Java?
The CopyOnWriteArraySet class is a part of the Java Collection Framework extends the AbstractSet class and implements the Serializable interface.
A CopyOnWriteArraySet is a thread-safe HashSet in Java and it works like CopyOnWriteArrayList in java. The CopyOnWriteArraySet internally used CopyOnWriteArrayList to perform all type of operation.It means the CopyOnWriteArraySet internally creates an object of CopyOnWriteArrayList and perform operation on it. Whenever we perform add, set, and remove operation on CopyOnWriteArraySet, it internally creates a new object of CopyOnWriteArrayList and copies all the data to the new object. So, when it is used in by multiple threads, it doesn’t create a problem, but it is well suited if you have small size collection and want to perform only read operation by multiple threads.
2. Important properties about CopyOnWriteArraySet ?
1. CopyOnWriteArraySet is a thread-safe variant of HashSet because whenever we perform any manipulation, it internally creates a new object and copies all the data to a new object.
2. It creates an object of CopyOnWriteArrayList internally and perform all the operations on it.
3. The CopyOnWriteArraySet is the best to use when we have only read operation in a multithreading environment because multiple threads can read it without creating a new copy.
4. The update operation is costly on CopyOnWriteArraySet because it creates a separate copy for each update operation
5. The remove operation doesn’t support by Iterator and it will throw Run-time exception UnsupportedOperationException.
6. The CopyOnWriteArraySet is the replacement of synchronized Set and offers better concurrency when iterations outnumber mutations.
7. Because it creates a new copy of the underlying array every time iterator is created, performance is slower than HashSet.
3. How to create CopyOnWriteArraySet?
We can create CopyOnWriteArraySet through the use of Constructor. The CopyOnWriteArraySet provides two constructors that are used to create CopyOnWriteArraySet. Both constructors internally create an object CopyOnWriteArrayList and perform an operation on the internal object. Let’s create CopyOnWriteArraySet by the use of both constructors.
4. Constructors of CopyOnWriteArraySet
- CopyOnWriteArraySet()
- CopyOnWriteArraySet(Collection c)
CopyOnWriteArraySet()
It is the default constructor of CopyOnWriteArraySet class, and it creates an empty set. When we create a set by use of default constructor it internally invokes the default constructor of CopyOnWriteArrayList class.
public CopyOnWriteArraySet() { al = new CopyOnWriteArrayList<E>(); }
Let’s takes an example
import java.util.concurrent.CopyOnWriteArraySet; public class CopyOnWriteArraySetExample { public static void main(String[] args) { CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<String>(); set.add("Java"); set.add("Goal"); set.add("Website"); System.out.println("Element from Set: "+ set); } }
Output: Element from Set: [Java, Goal, Website]
CopyOnWriteArraySet(Collection c)
It is the parameterized constructor of the CopyOnWriteArrayList class that accepts only one parameter of collection type. It is used to create a set containing all the elements of specified collection. If the collection contains duplicate elements, it accepts only unique elements. It throws NullPointerException if the given collection is null.
import java.util.ArrayList; import java.util.concurrent.CopyOnWriteArraySet; public class CopyOnWriteArraySetExample { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); list.add("Java"); list.add("Goal"); list.add("Website"); CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<String>(list); System.out.println("Element from Set: "+ set); } }
Output: Element from Set: [Java, Goal, Website]
5. Method of CopyOnWriteArraySet
1. add(E e) method: The add(E e) method is used to add the given element in the set. It returns a boolean value. It returns false if the element already exists in the set.
2. addAll(Collection c): The addAll(Collection c) method is used to add the given collection in Set. It adds all the elements of the collection in the set. If any element already exists or duplicate in the collection, then it doesn’t add the element. It throws NullPointerException if the given collection is null.
3. remove(Object o): This method is used to remove the given element from the set. It returns false if the element doesn’t exist.
4. clear(): This method removes all of the elements from set.
5. contains(Object o): It is used to check whether the element exists in set ot not
6. isEmpty(): This method is used to check whether the set is empty or not? It returns a boolean value.
7. iterator(): It returns an object of iterator that contains all the elements of set.
8. int size() : It returns the number of elements present in the set.
6. java Copyonwritearrayset example?
import java.util.concurrent.CopyOnWriteArraySet; public class CopyOnWriteArraySetExample { public static void main(String[] args) { CopyOnWriteArraySet<Integer> set = new CopyOnWriteArraySet<Integer>(); set.add(1); set.add(2); set.add(3); set.add(4); set.add(5); // Check element contains System.out.println("Is element contains: "+set.contains(1)); // Check set is empty System.out.println("Is set empty: "+set.isEmpty()); // remove element from set System.out.println("remove element from set: "+set.remove(3)); System.out.println("Element from Set: "+ set); } }
Output: Is element contains: true
Is set empty: false
remove element from set: true
Element from Set: [1, 2, 4, 5]