CopyOnWriteArrayList in Java

Last Updated : 13 Apr, 2026

CopyOnWriteArrayList is a class introduced in JDK 1.5 that implements the List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc.) are implemented by creating a fresh copy. It is found in the java.util.concurrent package and is designed to be used in a concurrent environment.

  • Thread-safe version of ArrayList suitable for concurrent access.
  • All update operations create a cloned copy of the underlying list.
  • Iterators provide a snapshot and do not throw ConcurrentModificationException.
Java
import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayListExample {
    public static void main(String[] args) {

        // Creating a CopyOnWriteArrayList
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();

        // Adding elements
        list.add("Java");
        list.add("Python");
        list.add("C++");

        // Printing the list
        System.out.println("CopyOnWriteArrayList elements: " + list);
    }
}

Output
CopyOnWriteArrayList elements: [Java, Python, C++]

Explanation:This example demonstrates how to create a CopyOnWriteArrayList, add elements using add() method, and display the elements.

Hierarchy of CopyOnWriteArrayList

CopyOnWriteArrayList extends the Object class and implements List, RandomAccess, Cloneable, and Serializable interfaces in the java.util.concurrent package.

collection
CopyOnWriteArrayList

Declaration of CopyOnWriteArrayList

public class CopyOnWriteArrayList<E> extends Object implements List<E>, RandomAccess, Cloneable, Serializable

  • Here, E is the type of elements held in this collection.
  • The class implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess interfaces.

Constructors Of CopyOnWriteArrayList

1. CopyOnWriteArrayList():

Creates an empty list.

CopyOnWriteArrayList<String> c = new CopyOnWriteArrayList<>();

2. CopyOnWriteArrayList(Collection obj):

Creates a list containing the elements of the specified collection.

CopyOnWriteArrayList<String> c = new CopyOnWriteArrayList<>(collection);

3. CopyOnWriteArrayList(Object[] obj):

Creates a list holding a copy of the given array.

CopyOnWriteArrayList<String> c = new CopyOnWriteArrayList<>(array);

Java
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class ConcurrentDemo extends Thread {

    static CopyOnWriteArrayList<String> l = new CopyOnWriteArrayList<>();

    public void run() {
        // Child thread adding new element
        l.add("D");
    }

    public static void main(String[] args) {

        l.add("A");
        l.add("B");
        l.add("C");

        ConcurrentDemo t = new ConcurrentDemo();
        t.start();

        // Iterating list
        Iterator<String> itr = l.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next());
        }

        System.out.println("Final List: " + l);
    }
}

Output
A
B
C
Final List: [A, B, C, D]

Explanation:In this example, a child thread adds a new element to the CopyOnWriteArrayList while the main thread iterates over it. The iterator works on a snapshot of the list, so the modification does not affect the current iteration and no ConcurrentModificationException occurs.

Methods of CopyOnWriteArrayList:

METHOD

DESCRIPTION

add(E e)Appends the specified element to the end of this list.
add(int index, E element)Inserts the specified element at the specified position in this list.
addAll(Collection<? extends E> c)Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
addAll(int index, Collection<? extends E> c)Inserts all of the elements in the specified collection into this list, starting at the specified position.
addAllAbsent(Collection<? extends E> c)Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's iterator.
addIfAbsent(E e)Appends the element, if not present.
clear()Removes all of the elements from this list.
clone()Returns a shallow copy of this list.
contains(Object o)Returns true if this list contains the specified element.
containsAll(Collection<?> c)Returns true if this list contains all of the elements of the specified collection.
 equals(Object o)Compares the specified object with this list for equality.
 forEach(Consumer<? super E> action)Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
 get(int index)Returns the element at the specified position in this list.
 hashCode()Returns the hash code value for this list.
indexOf(E e, int index)Returns the index of the first occurrence of the specified element in this list, searching forwards from the index, or returns -1 if the element is not found.
indexOf(Object o)Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
isEmpty()Returns true if this list contains no elements.
iterator()Returns an iterator over the elements in this list in the proper sequence.
lastIndexOf(E e, int index)Returns the index of the last occurrence of the specified element in this list, searching backward from the index, or returns -1 if the element is not found.
lastIndexOf(Object o)Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
listIterator()Returns a list iterator over the elements in this list (in proper sequence).
listIterator(int index)Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
remove(int index)Removes the element at the specified position in this list.
remove(Object o)Removes the first occurrence of the specified element from this list, if it is present.
removeAll(Collection<?> c)Removes from this list all of its elements that are contained in the specified collection.
 removeIf(Predicate<? super E> filter)Removes all of the elements of this collection that satisfy the given predicate.
replaceAll(UnaryOperator<E> operator)Replaces each element of this list with the result of applying the operator to that element.
retainAll(Collection<?> c)Retains only the elements in this list that are contained in the specified collection.
set(int index, E element)Replaces the element at the specified position in this list with the specified element.
 size()Returns the number of elements in this list.
sort(Comparator<? super E> c)Sorts this list according to the order induced by the specified Comparator.
spliterator()Returns a Spliterator over the elements in this list.
 subList(int fromIndex, int toIndex)Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.
toArray()Returns an array containing all of the elements in this list in proper sequence (from first to the last element).
 toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
 toString()Returns a string representation of this list.

Methods inherited from interface java.util.Collection:

METHOD

DESCRIPTION

parallelStream()Returns a possibly parallel Stream with this collection as its source.
stream()Returns a sequential Stream with this collection as its source.


Comment