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 | 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 |
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:
- Java 5 – Introduction to Concurrent Collection
- Java 5 – CopyOnWriteArrayList class with example
- Java 5 – CopyOnWriteArrayList with Read and Update operations simultaneously
- Java 5 – Remove operation with CopyOnWriteArrayList and ArrayList
- Java 5 – ArrayList v/s CopyOnWriteArrayList
- Java 5 – CopyOnWriteArrayList v/s SynchronizedList
- Java 5 – Concurrent Collection Interview question and answers
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html
- https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
Happy Coding !!
Happy Learning !!