CopyOnWriteArrayList v/s ArrayList

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

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

 

CopyOnWriteArrayList v/s ArrayList :

 CopyOnWriteArrayList ArrayList
CopyOnWriteArrayList is synchronized or newly introduced thread-safe class ArrayList 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 ArrayList

In multi-threaded environment, ArrayList is faster than CopyOnWriteArrayList as multiple threads can operate

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

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

And it never throws ConcurrentModificationException

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

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

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

 

When to use ArrayList ?

  • When there are more number of retrievals like accessing employee records against the employee code and
  • Insertion and deletion is very less (or very minimal)
  • Reason: internally when capacity exceeds then new array with 50% more than original size is created and older array data/items/elements are copied into new array
  • Similarly, lot of shifting while deleting/removing an item/element from ArrayList
  • But if 2 or more threads works on the same ArrayList object simultaneously; then compiler throws ConcurrentModificationException
  • Use ArrayList instead of CopyOnWriteArrayList, when there is no thread-safety required

When to use CopyOnWriteArrayList ?

  • This is the best suit to store 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 CopyOnWriteArrayList over ArrayList is that, it doesn’t throws ConcurrentModificationException when multiple threads performes operation simultaneously

ArrayList v/s CopyOnWriteArrayList :

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

 

References:

 

Happy Coding !!
Happy Learning !!

CopyOnWriteArrayList v/s SynchronizedList
Remove operation with CopyOnWriteArrayList