Java CopyOnWriteArray Example

Published May 20, 2022

CopyOnWriteArrayList is a part of the Java Collection framework that implements List, RandomAccess, Cloneable, and Serializable interfaces.

 CopyOnWriteArrayList

 

CopyOnWriteArrayLists are similar to ArrayLists; however, they have some additional features such as thread safety. This class is found in java.util.concurrent.

Since ArrayLists are not thread-safe, we cannot use them in the multithreaded environment, and therefore we use the CopyOnWriteArrayList class.

The CopyOnWriteArraList is usually an enhanced version of an ArrayList, and it is of great use if you want to read data more frequently.

CopyOnWriteArrayList Methods

Here is a list of the CopyOnWriteArrayList class's important methods:

  • boolean add(Object o): This method appends  the specified  element  to the end  of the list.

  • boolean addAll(Collection c): When the specified collection is null, this method appends all of the elements to the end of this List, in the order that the specified collection's iterator returns them.

  • boolean addAll(int index, Collection c): The method inserts all elements in the specified collection into this list, starting at the specified position. If the collection is null, a NullPointerException is thrown.

  • void clear():  Using this method, all List elements will be removed.

  • Object clone(): this method returns a shallow copy of this ArrayList.

  • boolean contains(Object o) : It returns true when list contains the given 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)).

  • object remove(int index)- This method removes the element at the specified position in this List and throws an IndexOutOfBoundsException if the index is out of range (index = size()).

  • object[] toArray()-  The method returns an array that contains all elements in the specified array in the correct order and throws NullPointerException if the array is null.

  •  int size() -  this method counts the number of elements in a list.

int lastIndexOf(Object o) - The method returns the index of the last occurrence of the specified element in the List, or -1 if the List does not contain this element.

 

Example

import java.util.Comparator;

import java.util.concurrent.CopyOnWriteArrayList;

public class CopyOnWriteArrayList_Example {

            public static void main(String[] args) {

                        CopyOnWriteArrayList<String> mycopyOnWriteArrayListObject = new CopyOnWriteArrayList<>();

                        mycopyOnWriteArrayListObject.add("python");

                        mycopyOnWriteArrayListObject.add("java");

                        mycopyOnWriteArrayListObject.add("kotlin");

                        mycopyOnWriteArrayListObject.add("Bootstrap");

        System.out.println("Our List without sorting - " + mycopyOnWriteArrayListObject);

        mycopyOnWriteArrayListObject.sort(Comparator.naturalOrder());

        System.out.println(" OurList with sorting - " + mycopyOnWriteArrayListObject);

            }

}

 

Output:

Our List without sorting - [python, java, kotlin, Bootstrap]

 OurList with sorting - [Bootstrap, java, kotlin, python]

 

Keywords: Java CopyOnWriteArray , Java Collections

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

134 Views