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 operations which can change the arraylist (add, update, set methods) creates a clone of the underlying array.

  • CopyOnWriteArrayList is to be used in Thread based environment where read operations are very frequent and update operations are rare.

  • Iterator of CopyOnWriteArrayList will never throw ConcurrentModificationException.

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

  • List modification methods like remove, set and add are not supported in iteration. These method will throw UnsupportedOperationException.

  • null can be added to the list.

CopyOnWriteArrayList Methods

Following is the list of important methods available in the CopyOnWriteArrayList class.

Sr.No.Method & Description
1void add(int index, Object element)
Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index > size()).
2boolean add(Object o)
Appends 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. Throws NullPointerException, if the specified collection is null.
4boolean addAll(int index, Collection c)
Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null.
5void clear()
Removes all of the elements from this list.
6Object clone()
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 e such that (o==null ? e==null : o.equals(e)).
8Object get(int index)
Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).
9int indexOf(Object o)
Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
10int lastIndexOf(Object o)
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
11Object remove(int index)
Removes the element at the specified position in this list. Throws IndexOutOfBoundsException if the index out is of range (index < 0 || index >= size()).
12Object set(int index, Object element)
Replaces the element at the specified position in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size()).
13int size()
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. Throws NullPointerException if the specified array is null.

Example

The following program illustrates several of the methods supported by CopyOnWriteArrayList.

 Live Demo

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 the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");
      al.add(1, "A2");
      System.out.println("Size of al after additions: " + al.size());
      // display the array list
      System.out.println("Contents of al: " + al);
      // Remove elements from the 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("Method not supported:");
      }
      System.out.println("Size of al: " + al.size());
   }
}

Output

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]
Method not supported:
Size of al: 5

Updated on: 26-Jun-2020

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements