CopyOnWriteArrayList in Java - GeeksforGeeks

05 Feb, 2021
CopyOnWriteArrayList class is introduced in JDK 1.5, which implements the List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy. It is found in java.util.concurrent package. It is a data structure created to be used in a concurrent environment. 
Here are few points about CopyOnWriteArrayList: 
As the name indicates, CopyOnWriteArrayList creates a Cloned copy of underlying ArrayList, for every update operation at a certain point both will be synchronized automatically, which is taken care of by JVM. Therefore, there is no effect for threads that are performing read operation.
It is costly to use because for every update operation a cloned copy will be created. Hence, CopyOnWriteArrayList is the best choice if our frequent operation is read operation.
The underlined data structure is a grow-able array.
It is a thread-safe version of ArrayList.
Insertion is preserved, duplicates, null, and heterogeneous Objects are allowed.
The main important point about CopyOnWriteArrayList is the Iterator of CopyOnWriteArrayList can not perform remove operation otherwise we get Run-time exception saying UnsupportedOperationException. add() and set() methods on CopyOnWriteArrayList iterator also throws UnsupportedOperationException. Also Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.
Declaration:
public class CopyOnWriteArrayList<E> extends Object implements List<E>, RandomAccess, Cloneable, Serializable
Here, E is the type of elements held in this collection.
Note: The class implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess interfaces.
Constructors:
1. CopyOnWriteArrayList(): Creates an empty list.
CopyOnWriteArrayList c = new CopyOnWriteArrayList();
2. CopyOnWriteArrayList(Collection obj): Creates a list containing the elements of the specified collection, in the order, they are returned by the collection’s iterator.
CopyOnWriteArrayList c = new CopyOnWriteArrayList(Collection obj);
3. CopyOnWriteArrayList(Object[] obj);: Creates a list holding a copy of the given array.
CopyOnWriteArrayList c = new CopyOnWriteArrayList(Object[] obj);
Example: 
Java
// Java program to illustrate// CopyOnWriteArrayList classimport java.util.*;import java.util.concurrent.CopyOnWriteArrayList; public class ConcurrentDemo extends Thread {     static CopyOnWriteArrayList<String> l        = new CopyOnWriteArrayList<String>();     public void run()    {        // Child thread trying to        // add new element in the        // Collection object        l.add("D");    }     public static void main(String[] args)        throws InterruptedException    {        l.add("A");        l.add("B");        l.add("c");         // We create a child thread        // that is going to modify        // ArrayList l.        ConcurrentDemo t = new ConcurrentDemo();        t.run();         Thread.sleep(1000);         // Now we iterate through        // the ArrayList and get        // exception.        Iterator itr = l.iterator();        while (itr.hasNext()) {            String s = (String)itr.next();            System.out.println(s);            Thread.sleep(1000);        }        System.out.println(l);    }}
A
B
c
D
[A, B, c, D]

 
Iterating over CopyOnWriteArrayList: We can iterate over CopyOnWriteArrayList using iterator() method. The important point to be noted is that the iterator we create is an immutable snapshot of the original list. Because of this property, we can see that GfG is not printed at the first iteration.
Java
// Java program to illustrate// CopyOnWriteArrayList classimport java.io.*;import java.util.*;import java.util.concurrent.*; class Demo {    public static void main(String[] args)    {         CopyOnWriteArrayList<String> list            = new CopyOnWriteArrayList<>();         // Initial Iterator        Iterator itr = list.iterator();        list.add("GfG");        System.out.println("List contains: ");        while (itr.hasNext())            System.out.println(itr.next());         // iterator after adding an element        itr = list.iterator();        System.out.println("List contains:");        while (itr.hasNext())            System.out.println(itr.next());    }}
List contains: 
List contains:
GfG

Methods of CopyOnWriteArrayList:
METHOD
DESCRIPTION
Methods inherited from interface java.util.Collection:
METHOD
DESCRIPTION
Note: We should use CopyOnWriteArrayList when we prefer to use a data structure similar to ArrayList in a concurrent environment.
Must Read:
Difference between ArrayList and CopyOnWriteArrayList
vinaykumar54
randhish79
rahulcooldude05
Ganeshchowdharysadanala
ankitkadam11
Java - util package
Java-ArrayList
Java-Collections
Java-CopyOnWriteArrayList
java-list
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java
Initialize an ArrayList in Java
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java