CopyOnWriteArrayList class

  • September 10, 2022
  • Java
java thread 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

Leave a Reply

Your email address will not be published. Required fields are marked *