CopyOnWriteArrayList in java

CopyOnWriteArrayList: CopyOnWriteArrayList class is introduced in JDK 1.5, which implements List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy.

 

Here are few points about CopyOnWriteArrayList:

  • As the name indicates, CopyOnWriteArrayList creates a Cloned copy of underlying ArrayList, for every update operation at a certain point both will be synchronized automatically, which is taken care of by JVM. Therefore there is no effect for threads that are performing read operation.
  • It is costly to use because for every update operation a cloned copy will be created. Hence CopyOnWriteArrayList is the best choice if our frequent operation is read operation.
  • It extends object and implements Serializable, Cloneable, Iterable<E>, Collection<E>, List<E> and RandomAccess
  • The underlined data structure is a grow-able array.
  • It is a thread-safe version of ArrayList.
  • Insertion is preserved, duplicates are allowed and heterogeneous Objects are allowed.
  • The main important point about CopyOnWriteArrayList is the Iterator of CopyOnWriteArrayList can not perform remove operation otherwise we get Run-time exception saying UnsupportedOperationException.

Constructors Summary:

  • CopyOnWriteArrayList c = new CopyOnWriteArrayList(); :Creates an empty list.
  • CopyOnWriteArrayList c = new CopyOnWriteArrayList(Collection obj);:Creates a list containing the elements of the specified collection, in the order they are returned by the collection’s iterator.
  • CopyOnWriteArrayList c = new CopyOnWriteArrayList(Object[] obj);:Creates a list holding a copy of the given array.

Methods Summary:

  1. add(int index, E element): This method Inserts the specified element at the specified position in this list.
  2. add(E e): This method Appends the specified element to the end of this list.
  3. addAll(int index, Collection<E> c): This method Inserts all of the elements in the specified collection into this list, starting at the specified position.
  4. addAll(Collection<E> c): This method 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.
  5. addAllAbsent(Collection<E> c): This method Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection’s iterator.
  6. addIfAbsent(E e): This method Appends the element, if not present.
  7. clear(): This method Removes all of the elements from this list.
  8. clone(): This method returns a shallow copy of this list.
  9. contains(Object o): This method returns true if this list contains the specified element.
  10. contains(Collection<E> c): This method returns true if this list contains all of the elements of the specified collection.
  11. equals(Object o): This method Compares the specified object with this list for equality.
  12. forEach(Consumer<E> action): This method Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
  13. get(int index): This method returns the element at the specified position in this list.
  14. hashCode(): This method returns the hash code value for this list.
  15. indexOf(E e, int index): This method returns the index of the first occurrence of the specified element in this list, searching forwards from the index, or returns -1 if the element is not found.
  16. indexOf(Object o): This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
  17. isEmpty(): This method returns true if this list contains no elements.
  18. iterator(): This method returns an iterator over the elements in this list in a proper sequence.
  19. lastIndexOf(E e, int index): This method returns the index of the last occurrence of the specified element in this list, searching backward from the index, or returns -1 if the element is not found.
  20. lastIndexOf(Object o): This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
  21. list iterator(): This method returns a list iterator over the elements in this list (in proper sequence).
  22. list iterator(int index): This method returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
  23. remove(int index): This method Removes the element at the specified position in this list.
  24. remove(Object o): This method Removes the first occurrence of the specified element from this list if it is present.
  25. removeAll(Collection<E> c): This method Removes from this list all of its elements that are contained in the specified collection.
  26. remove(Predicate<E> filter): This method Removes all of the elements of this collection that satisfy the given predicate.
  27. replaceAll(UnaryOperator<E> operator): This method Replaces each element of this list with the result of applying the operator to that element.
  28. retain(Collection<E> c): This method Retains only the elements in this list that are contained in the specified collection.
  29. set(int index, E element): This method Replaces the element at the specified position in this list with the specified element.
  30. size(): This method returns the number of elements in this list.
  31. sort(Comparator<E> c): This method Sorts this list according to the order induced by the specified Comparator.
  32. spliterator(): This method returns a Spliterator over the elements in this list.
  33. subList(int fromIndex, int toIndex): This method returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.
  34. toArray(): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element).
  35. toArray(T[] a): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
  36. toString(): This method returns a string representation of this list.

filter_none

edit

play_arrow

brightness_4

// Java program to illustrate

// CopyOnWriteArrayList class

import java.util.concurrent.CopyOnWriteArrayList;

import java.util.*;

  

class ConcurrentDemo extends Thread {

  

    static CopyOnWriteArrayList l = new CopyOnWriteArrayList();

  

    public void run()

    {

        // Child thread trying to

        // add new element in the

        // Collection object

        l.add("D");

    }

  

    public static void main(String[] args)

        throws InterruptedException

    {

        l.add("A");

        l.add("B");

        l.add("c");

  

        // We create a child thread

        // that is going to modify

        // ArrayList l.

        ConcurrentDemo t = new ConcurrentDemo();

        t.run();

  

        Thread.sleep(1000);

  

        // Now we iterate through

        // the ArrayList and get

        // exception.

        Iterator itr = l.iterator();

        while (itr.hasNext()) {

            String s = (String)itr.next();

            System.out.println(s);

            Thread.sleep(1000);

        }

        System.out.println(l);

    }

}

Output:

A
B
c
D
[A, B, c, D]

Related Articles

post a comment