CopyOnWriteArrayList and the ArrayList are part of the Collections framework. CopyOnWriteArrayList and ArrayList implement the List interface (just like Vector and LinkedList), but underlying implementations either support multi-threaded applications or single threaded applications.

In CopyOnWriteArrayList, each mutative operation e.g. add, set etc are done after creating a copy of the underlying array. Since it uses a ‘snapshot’ style iterator and copy never changes for the lifetime of the iterator, it does not get a ConcurrentModificationException. In this lifetime, any modifications on the original one will not be available to that iterator which is working on the copy. You cannot use its iterator to do mutations otherwise it will return UnsupportedOperationException.

Both support generics, so any element can be added including null.

In ArrayList, if one thread if try to iterate over the list, while another one is modifying the list e.g. adding a new element or setting some element to a different value, then we are bound to get ConcurrentModificationException. Now, in contrast to this CopyOnWriteArrayList does not give mutation rights to anyone for the lifetime of the iterator.

CopyOnWriteArrayList is highly efficient if the number of iterations is far higher (upwards of 90+% reads) than the number of mutations. If most of the operations are only random access based then it is better to use ReadWriteLock.

ArrayList was introduced in JDK 1.2 whereas CopyOnWriteArrayList was introduced in JDK 1.5.

Let’s take an example of CopyOnWriteArrayList and its inter-workings with its iterator.

Products from Amazon.co.uk

In conclusion, for thread-safe iteration in lists, you have the option to use CopyOnWriteArrayList.