Java 5 – CopyOnWriteArraySet v/s HashSet

In this article, we will discuss difference between CopyOnWriteArraySet and HashSet classes in detail i.e.; CopyOnWriteArraySet v/s HashSet

Lets us move on and discuss key differences between these 2 Set classes

1. CopyOnWriteArraySet v/s HashSet :

  • COWAL –> CopyOnWriteArrayList
  • COWAS –> CopyOnWriteArraySet
CopyOnWriteArraySetHashSet
CopyOnWriteArraySet is synchronized and it is internally implemented using COWALHashSet is not synchronized
For every update operation, a new separate cloned copy is created and there is memory & merging overhead for JVM

 

Hence, performance is relatively low when comparing with HashSet

In multi-threaded environment, HashSet is faster than CopyOnWriteArraySet as multiple threads can operate

 

Hence, performance is high as there is no need to acquire lock

While one thread iterating CopyOnWriteArraySet items, other threads happily can modify, as it works on separate cloned copy

 

And it never throws ConcurrentModificationException

While one thread iterating HashSet items, if any other thread tries to modify same HashSet object then ConcurrentModificationException is thrown
That’s it is fail-safe iteratorThat’s it is fail-fast iterator
Iterator of CopyOnWriteArraySet can perform read operation safely; while iterating through COWAS items

 

But as soon as, remove operation is performed, compiler throws UnsupportedOperationException

Iterator of HashSet can perform both read and remove operations; while iterating through HashSet elements
Present in java.util.concurrent package and qualified class name is java.util.concurrent
.CopyOnWriteArraySet
Present in java.util package and qualified class name is java.util.HashSet
This is introduced in Java 1.5 versionThis is introduced in original collection framework in Java 1.2 version

Q) When to use HashSet ?

  • HashSet stores unique elements using hashing technique
  • So, search operation is faster
  • So, if business requirement is to store unique elements for faster search operation or more number of search operation without concerning insertion order
  • Then, HashSet is the very apt choice

Q) When to use CopyOnWriteArraySet ?

  • This is the best suit to store unique elements as per insertion order in a multi-threaded environment
  • Where there are more number of read operation and very less update/modify operation
  • Because for every update/modify operations, a new separate cloned copy is created
  • And there is overhead on JVM to allocate memory & merging cloned copy with original copy
  • The advantage of using CopyOnWriteArraySet over HashSet is that, it doesn’t throws ConcurrentModificationException when multiple threads performs operation simultaneously

2. CopyOnWriteArraySet v/s HashSet :

  • there is always a catch between performance and thread-safety
  • choose wisely for your requirement

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 5 - CopyOnWriteArraySet v/s SynchronizedSet
Java 5 - CopyOnWriteArraySet class with example