Java Collection Framework - CopyOnWriteArrayList class

Java Collection Framework - CopyOnWriteArrayList class


Posted in : Core Java Posted on : March 25, 2011 at 7:13 PM Comments : [ 0 ]

The CopyOnWriteArrayList class implements the List, Serializable, Cloneable, Iterable, Collection, and RandomAccess interfaces.

Java Collections Framework- CopyOnWriteArrayList class 

The CopyOnWriteArrayList class implements the List, Serializable, Cloneable, Iterable<E>, Collection<E>, and RandomAccess interfaces. CopyOnWriteArrayList is a thread safe implementation. In this class all the operations that alter the contents of a CopyOnWriteArrayList collection cause this array is to be replaced with a copy of itself before the contents of the array are changed that is this class keeps a copy of the original List and iterate over this and the new value which has been added is merged to copy of original array only after Iteration is over. Object of the CopyOnWriteArrayList that creates an Iterator can't alter the underlying array. Though methods of these iterators do changes the underlying collection, throws an UnsupportedOperationException instead of modifying the underlying collection. This class permits to all elements except the 'null'.

Constructor of CopyOnWriteArrayList class are :

CopyOnWriteArrayList() : This constructor makes an empty list.

CopyOnWriteArrayList(Collection<? extends E> c) : This constructor makes a list that keeps the elements of a particular collection, in the order they are backed by the collection's iterator.

CopyOnWriteArrayList(E[] toCopyIn) : This constructor makes a list that holds a transcript of the given array.

Syntax

public class CopyOnWriteArrayList<E>

Parameter description

E : It is the element's type that is held by this collection.

CopyOnWriteArrayList class supports some methods these are :

  • add(E e) 
  • add(int index, E element) 
  • addAll(Collection<? extends E> c)
  • addAll(int index, Collection<? extends E> c)
  • addAllAbsent(Collection<? extends E> c)
  • addIfAbsent(E e)
  • clear()
  • clone()
  • contains(Object o)
  • containsAll(Collection c)
  • equals(Object o)
  • get(int index)
  • hashCode()
  • indexOf(E e, int index)
  • indexOf(Object o)
  • isEmpty()
  • iterator()
  • lastIndexOf(E e, int index)
  • lastIndexOf(Object o)
  • listIterator()
  • listIterator(int index)
  • remove(int index)
  • remove(Object o)
  • removeAll(Collection c)
  • retainAll(Collection c)
  • set(int index, E element)
  • size()
  • subList(int fromIndex, int toIndex)
  • toArray()
  • toArray(T[] a)
  • toString()

Example1 :

Here we will discuss the example of the class CopyOnWriteArrayList.

package devmanuals.com;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.*;
public class CopyOnWriteArrayListDemo {
  public static void main (String args[]){
    CopyOnWriteArrayList list = new CopyOnWriteArrayList();
    list.add("DEV");
    list.add("MANUALS");
    list.add(".COM");
    System.out.println(list);
    list.add(3,"Rose");
    System.out.println("list = "+list);
    List list1 =new ArrayList();
    list1.add("A");
    list1.add("B");
    list1.add("E");
    System.out.println("list1 = "+list1);
    list.addAll(list1);
    System.out.println("Add all the elements of list1 to the list = "+list);
    list1.add("D");
    list1.add("B");
    list1.add("E");
    list.addAllAbsent(list1);
    System.out.println("Adds all the unavailable element then list = "+list);
    list.addIfAbsent("C");
    System.out.println("Adds the unavailable element then list = "+list);
    Object obj=list.clone();
    System.out.println("Clone of list = "+obj);
    boolean bol=list.contains("C");
    System.out.println("Does list contains 'C' : "+bol);
    boolean bol1 = list1.containsAll(list);
    System.out.println("Does list1 contains all of the elements of list :+bol1);
    boolean bol2=list1.equals(list);
    System.out.println(bol2);
    Object obj1 =list.get(3);
    System.out.println(obj1);
    Integer it =list.hashCode();
    System.out.println(it);
    int i=list.indexOf("ROSE",0);
    System.out.println("Index of first occurrence of ROSE = "+i);
    int i1=list.indexOf("Rose");
    System.out.println("Index of first occurrence of Rose = "+i1);
    boolean bol3 =list.isEmpty();
    System.out.println("Is list empty : "+bol3);
    Iterator itr = list.iterator();
    while(itr.hasNext()){
      System.out.println(itr.next());
    }
    int it1 = list.lastIndexOf("MANUALS"4);
    System.out.println("Index of last occurrence of MANUALS = "+it1);
    int it2 =list.lastIndexOf("A");
    System.out.println("Index of last occurrence of A = "+it2);
    ListIterator litr =list.listIterator();
    System.out.println("Elements in forward order : ");
    while(litr.hasNext()){
      System.out.println(litr.next());
    }
    System.out.println("Elements in reverse order : ");
    while(litr.hasPrevious()){
      System.out.println(litr.previous());
    }
  }
}

Output :

[DEV, MANUALS, .COM]

list = [DEV, MANUALS, .COM, Rose]

list1 = [A, B, E]

Add all the elements of list1 to the list =
          [DEV, MANUALS, .COM, Rose, A, B, E]

Adds all the unavailable element then list =
                [DEV, MANUALS, .COM, Rose, A, B, E, D]

Adds the unavailable element then list =
                [DEV, MANUALS, .COM, Rose, A, B, E, D, C]

Clone of list = [DEV, MANUALS, .COM, Rose, A, B, E, D, C]

Does list contains 'C' : true

Does list1 contains all of the elements of list : false

false

Rose

-827122162

Index of first occurrence of ROSE = -1

Index of first occurrence of Rose = 3

Is list empty : false

DEV

MANUALS

.COM

Rose

A

B

E

D

C

Index of last occurrence of MANUALS = 1

Index of last occurrence of A = 4

Elements in forward order :

DEV

MANUALS

.COM

Rose

A

B

E

D

C

Elements in reverse order :

C

D

E

B

A

Rose

.COM

MANUALS

DEV

Download Source Code

There is one more example which implements the methods of CopyOnWriteArrayList class.

package devmanuals.com;
import java.util.List;
import java.util.concurrent.*;
import java.util.ListIterator;
public class CopyOnWriteArrayListDemo1 {
  public static void main (String args[]){
    CopyOnWriteArrayList list = new CopyOnWriteArrayList();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    list.add("E");
    System.out.println("List of elements = "+list);
    ListIterator litr = list.listIterator(2);
    System.out.println("List of elements from the index '2' = ");
    while(litr.hasNext()){
      System.out.println(litr.next());
    }
    list.remove(2);
    list.remove("D");
    System.out.println("Rest elements of list = "+list);
    CopyOnWriteArrayList list1 = new CopyOnWriteArrayList();
    list1.add("A");
    list1.add("B");
    list1.add("C");
    boolean bol= list.retainAll(list1);
    System.out.println("Is list1 retains elements of list : "+bol);
    Object obj = list.set(1,"C");
    System.out.println(list);
    CopyOnWriteArrayList list2 = new CopyOnWriteArrayList();
    list2.add("A");
    list2.add("B");
    list2.add("C");
    list2.add("D");
    list2.add("E");
    List l = list2.subList(14);
    System.out.println("Sublist = "+l);
}
}

Output :

List of elements = [A, B, C, D, E]

List of elements from the index '2' =

C

D

E

Rest elements of list = [A, B, E]

Is list1 retains elements of list : true

[A, C]

Sublist = [B, C, D]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics