CopyOnWriteArrayList Class in Java


Class declaration

public class CopyOnWriteArrayList
   extends Object
implements List, 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 a 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 the iteration. This 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
1
void 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 size()).

2
boolean add(Object o)

Appends the specified element to the end of this list.

3
boolean 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.

4
boolean 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.

5
void clear()

Removes all of the elements from this list.

6
Object clone()

Returns a shallow copy of this ArrayList.

7
boolean 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)).

8
Object get(int index)

Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index = size()).

9
int 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.

10
int 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.

11
Object remove(int index)

Removes the element at the specified position in this list. Throws IndexOutOfBoundsException if the index out is of range (index = size()).

12
Object 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 = size()).

13
int size()

Returns the number of elements in this list.

14
Object[] 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 ArrayList −

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 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 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());
   }
}

This will produce the following result −

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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 19-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements