Java 5 – 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

1. CopyOnWriteArrayList v/s ArrayList :

CopyOnWriteArrayList is synchronized or newly introduced thread-safe classArrayList 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 iteratorThat’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
Present in java.util package and qualified class name is java.util.ArrayList
This is introduced in Java 1.5 versionThis is introduced in original collection framework in Java 1.2 version

Q) 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

Q) 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

2. ArrayList v/s CopyOnWriteArrayList :

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

Related Articles:


Happy Coding !!
Happy Learning !!

Java 5- CopyOnWriteArrayList v/s SynchronizedList
Java 5 - Remove operation with CopyOnWriteArrayList