Java 5 – CopyOnWriteArraySet class with example

In this article, we will discuss CopyOnWriteArraySet class – the implementation class for Set interface in detail

This is the thread-safe version of Set and it’s internally implemented using CopyOnWriteArrayList, where modify operation is performed on a separate cloned copy and finally JVM merges both original/cloned copies

As it is internally implemented using COWAL, therefore all properties of COWAL are applicable to COWAS also except COWAS doesn’t allow duplicate objects

  • COWAL –> CopyOnWriteArrayList
  • COWAS –> CopyOnWriteArraySet

1. CopyOnWriteArraySet:

  • CopyOnWriteArraySet is implementation class of Set interface (i.e.; CopyOnWriteArraySet implements Set)
  • Internally COWAS is implemented using COWAL
  • So for every modify/update operation, a new separate cloned copy is created and modification is performed on cloned copy; while other threads can iterate over original copy
  • After modification/updation, JVM takes care of merging both copies (i.e.; original and cloned copy) –> so that we get latest copy with all updation/modification
  • Since, every time a new separate cloned copy is created for updation/modification. Therefore, it is suited for multi-threaded environment where there are more read/get operation and comparatively less update/modify operation
  • While one thread iterating over original copy, other threads can modify with separate cloned copy and compiler won’t throw any ConcurrentModificationException; which isn’t case with other Set implemented classes like HashSet/TreeSet
  • It never throws ConcurrentModificationException while 2 or more threads operating simultaneously i.e.; it is fail-safe iterator
  • But, there are certain limitation too with CopyOnWriteArraySet which isn’t case with Set implemented classes like HashSet or TreeSet like while iterating COWAS, remove operation isn’t possible and compiler throws UnsupportedOperationException
  • Other than above discussed points, all other properties of COWAL are applicable for COWAS too i.e.;
  • Insertion-order is maintained, as it is internally implemented by COWAL
  • Duplicate objects are NOT allowed (this is one of the major difference between COWAL & COWAS)
  • Null insertion is possible
  • This is introduced in Java 1.5 version
  • Present in java.util.concurrent package and implements java.util.Set
  • Also, implements java.io.Serializable marker interfaces which provides special ability to CopyOnWriteArraySet (provided by JVM at run time) like,
    java.io.Serializable: to transfer objects across network
12-copyonwritearrayset-in-java

Source: Team BenchResources.Net

2. CopyOnWriteArraySet constructors:

2.1 CopyOnWriteArraySet cowas = new CopyOnWriteArraySet();

  • creates an empty CopyOnWriteArraySet object

2.2 CopyOnWriteArraySet cowas = new CopyOnWriteArraySet(Collection c);

  • creates an equivalent CopyOnWriteArraySet object for the specified collection
  • it is basically used for inter-conversion between collection objects

3. CopyOnWriteArraySet Example:

CopyOnWriteArraySetDemo.java

package in.bench.resources.concurrent.collection;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;

public class CopyOnWriteArraySetDemo {

	public static void main(String[] args) {

		// // creating CopyOnWriteArraySet object of type String
		CopyOnWriteArraySet<String> cowas =
				new CopyOnWriteArraySet<String>();

		// adding elements to COWAS
		cowas.add("Mumbai");
		cowas.add("Tokyo");
		cowas.add("Shanghai");
		cowas.add(null); // 1st NULL
		cowas.add("Melbourne");
		cowas.add("Mumbai"); // duplicate
		cowas.add("Centurion");
		cowas.add(null); // 2nd NULL
		cowas.add("Singapore");
		cowas.add("Tokyo"); // duplicate

		System.out.println("CopyOnWriteArraySet demo example:\n");

		// get Iterator object
		Iterator<String> cities = cowas.iterator();

		// iterate using while-loop
		while(cities.hasNext()){
			System.out.println(cities.next());
		}
	}
}

Output:

CopyOnWriteArraySet demo example:

Mumbai
Tokyo
Shanghai
null
Melbourne
Centurion
Singapore

From above output, it is clear that,

  • COWAS maintains insertion-order
  • Doesn’t allows duplicate elements
  • Null insertion is possible with max cap of one

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 5 - CopyOnWriteArraySet v/s HashSet
Java 5- CopyOnWriteArrayList v/s SynchronizedList