Categories: Java

CopyOnWriteArrayList class

The CopyOnWriteArrayList class, which implements the List interface, is introduced in JDK 1.5. It is an enhanced version of ArrayList in which all changes (add, set, remove, and so on) are implemented by making a new copy. It is included in the java.util.concurrent package. Furthermore, it is a data structure that was created for use in a multi-threaded environment.
CopyOnWriteArrayList is a thread-safe ArrayList variant in which operations that change the ArrayList (add, update, and set methods) create a clone of the underlying array.

CopyOnWriteArrayList is intended for use in a Thread-based environment where read operations are common, but update operations are uncommon.

Methods of CopyOnWriteArrayListClass:
Method Description
void add(int index, Object element)
Inserts the specified element at the specified position in this list.Throws IndexOutOfBoundsException if the specified index is out of range (index size()).
boolean add(Object o)

The specified element is appended to the end of this list.
boolean addAll(Collection c)All elements from the specified collection are appended to the end of this list in the order in which they are returned by the specified collection’s iterator. If the specified collection is null, a NullPointerException is thrown.
boolean addAll(int index, Collection c)Starts at the specified position and inserts all of the elements from the specified collection into this list. If the specified collection is null, a NullPointerException is thrown.
void clear()This function removes all of the elements from this list.
Object clone()This method returns a shallow copy of this ArrayList.
boolean contains(Object o)
If this list contains the specified element, this function returns true. In more formal terms, this function returns true if and only if this list contains at least one element e such that (o==null? e==null: o.equals(e)).
Object get(int index)
The element at the specified position in this list is returned. If the specified index is out of range (index = size()), an IndexOutOfBoundsException is thrown.
int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if the List does not contain this element.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if the List does not contain this element.
Object remove(int index)
The element at the specified position in this list is removed.Throws IndexOutOfBoundsException if the index out is of range (index = size()).
Object set(int index, Object element)
The specified element replaces the element at the specified position in this list. If the specified index is out of range (index = size()), an IndexOutOfBoundsException is thrown.
int size()
Returns the number of elements in this list.
Object[] toArray()
Returns an array with all the elements in this list in the correct order. If the specified array is null, a NullPointerException is thrown.
Example:
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class Coderz {

   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("Code");
      al.add("Alt");
      al.add("Extra");
      al.add("Binary");
      al.add("Dual");
      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());
   }
}
Output:
Initial size of al: 0
Size of al after additions: 7
Contents of al: [Code, A2, Alt, Extra, Binary, Dual, F]
Size of al after deletions: 5
Contents of al: [Code, A2, Extra, Binary, Dual]
Method not supported:
Size of al: 5

Note: also read about the AbstractSequential List in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago