English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The CopyOnWriteArrayList class in Java programming

Class Declaration

public class CopyOnWriteArrayList<E>
extends Object
implements List<E>, RandomAccess, Cloneable, Serializable
  • CopyOnWriteArrayList is a thread-safe variant of ArrayList, where the operations that can change the ArrayList (add, update, set methods) create a clone of the underlying array.

  • CopyOnWriteArrayList is used in a thread-based environment where read operations are very frequent and update operations are rare.

  • The iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.

  • Any type of modification to CopyOnWriteArrayList will not be reflected during iteration since the iterator was created.

  • List modification methods are not supported during iteration, such as remove, set, and add. These methods will throw UnsupportedOperationException.

  • Null can be added to the list.

CopyOnWriteArrayList Methods

The following is a list of important methods available in the CopyOnWriteArrayList class.

Serial NumberMethod and Description
1void add(int index, Object element)
Insert the specified element at the specified index in this list. If the specified index is out of range (index < 0 || index size()),then an IndexOutOfBoundsException is thrown.
2boolean add(Object o)
Append the specified element to the end of this list.
3boolean addAll(Collection 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. If the specified collection is null, then a NullPointerException is thrown.
4boolean addAll(int index, Collection c)
From the specified position, inserts all of the elements in the specified collection into this list. If the specified collection is null, then a NullPointerException is thrown.
5voidclear()
Removes all elements from this list.
6Objectclone()
Returns a shallow copy of this ArrayList.
7boolean contains(Object o)
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element ËSatisfies (o == NULL || o.equals(e))
8Object get(int index)
Returns the element at the specified position in this list. If the specified index is out of range (index < 0 || index >= size()),then an IndexOutOfBoundsException is thrown.
9int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list; if the list does not contain the element, then returns-1.
10int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list; if the list does not contain the element, then returns-1.
11Object remove(int index)
Removes the element at the specified position in this list. If the index is out of range (index < 0 || index >= size()),then an IndexOutOfBoundsException is thrown.
12Object set(int index, Object element)
Replaces the element at the specified position in this list with the specified element. If the specified index is out of range (index < 0 || index >= size()),then an IndexOutOfBoundsException is thrown.
13intsize()
Returns the number of elements in this list.
14Object[]toArray()
Returns an array containing all of the elements in this list in the correct order. If the specified array is null, then a NullPointerException is thrown.

Example

The following program illustrates several methods supported by CopyOnWriteArrayList.

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class Tester {}}
   public static void main(String args[]) {
      //Create an array list
      CopyOnWriteArrayList<String> al = new CopyOnWriteArrayList();
      System.out.println("Initial size of al: "); + al.size());
      //Add elements to array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, \2");
      System.out.println("Size of al after additions: "); + al.size());
      //Display array list
      System.out.println("Contents of al: "); + al);
      //Remove elements from array list
      al.remove("F");
      al.remove(2);
      System.out.println("Size of al after deletions: "); + al.size());
      System.out.println("Contents of al: "); + al);
      try {
         Iterator<String> iterator = al.iterator();
         while(iterator.hasNext()) {
            iterator.remove();
         }
      }catch(UnsupportedOperationException e) {
         System.out.println("Unsupported methods:");
      }
      System.out.println("Size of al: "); + al.size());
   }
}

Output result

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
Unsupported methods:
Size of al: 5