Java Program to Implement CopyOnWriteArrayList API

This Java program Implements CopyOnWriteArrayList API.A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

Here is the source code of the Java Program to Implement CopyOnWriteArrayList API.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Collection;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6. import java.util.Set;
  7. import java.util.concurrent.CopyOnWriteArrayList;
  8.  
  9. public class CopyOnWriteArrayListImpl<E>
  10. {
  11.     private CopyOnWriteArrayList<E> copyOnWriteArrayList;
  12.  
  13.     /** Creates an empty list. **/
  14.     public CopyOnWriteArrayListImpl()
  15.     {
  16.         copyOnWriteArrayList = new CopyOnWriteArrayList<>();
  17.     }
  18.  
  19.     /**
  20.      * Creates a list containing the elements of the specified collection, in
  21.      * the order they are returned by the collection's iterator.
  22.      **/
  23.     public CopyOnWriteArrayListImpl(Collection<? extends E> c)
  24.     {
  25.         copyOnWriteArrayList = new CopyOnWriteArrayList<E>(c);
  26.     }
  27.  
  28.     /** Creates a list holding a copy of the given array. **/
  29.     public CopyOnWriteArrayListImpl(E[] tocopyIn)
  30.     {
  31.         copyOnWriteArrayList = new CopyOnWriteArrayList<E>(tocopyIn);
  32.     }
  33.  
  34.     /** Appends the specified element to the end of this list. **/
  35.     public boolean add(E e)
  36.     {
  37.         return copyOnWriteArrayList.add(e);
  38.     }
  39.  
  40.     /** Inserts the specified element at the specified position in this list. **/
  41.     public void add(int index, E element)
  42.     {
  43.         copyOnWriteArrayList.add(index, element);
  44.     }
  45.  
  46.     /**
  47.      * Appends all of the elements in the specified collection to the end of
  48.      * this list, in the order that they are returned by the specified
  49.      * collection's iterator.
  50.     **/
  51.     public boolean addAll(Collection<? extends E> c)
  52.     {
  53.         return copyOnWriteArrayList.addAll(c);
  54.     }
  55.  
  56.     /**
  57.      * Inserts all of the elements in the specified collection into this list,
  58.      * starting at the specified position.
  59.     **/
  60.     public boolean addAll(int index, Collection<? extends E> c)
  61.     {
  62.         return copyOnWriteArrayList.addAll(index, c);
  63.     }
  64.  
  65.     /**
  66.      * Appends all of the elements in the specified collection that are not
  67.      * already contained in this list, to the end of this list, in the order
  68.      * that they are returned by the specified collection's iterator.
  69.     **/
  70.     public int addAllAbsent(Collection<? extends E> c)
  71.     {
  72.         return copyOnWriteArrayList.addAllAbsent(c);
  73.     }
  74.  
  75.     /** Append the element if not present. **/
  76.     public boolean addIfAbsent(E e)
  77.     {
  78.         return copyOnWriteArrayList.addIfAbsent(e);
  79.     }
  80.  
  81.     /** Removes all of the elements from this list. **/
  82.     public void clear()
  83.     {
  84.         copyOnWriteArrayList.clear();
  85.     }
  86.  
  87.     /** Returns a shallow copy of this ArrayList instance. **/
  88.     public Object clone()
  89.     {
  90.         return copyOnWriteArrayList.clone();
  91.     }
  92.  
  93.     /** Returns true if this list contains the specified element. **/
  94.     public boolean contains(Object o)
  95.     {
  96.         return copyOnWriteArrayList.contains(o);
  97.     }
  98.  
  99.     /** Returns the element at the specified position in this list. **/
  100.     public E get(int index)
  101.     {
  102.         return copyOnWriteArrayList.get(index);
  103.     }
  104.  
  105.     /**
  106.      * Returns the index of the first occurrence of the specified element in
  107.      * this list, or -1 if this list does not contain the element.
  108.     **/
  109.     public int indexOf(Object o)
  110.     {
  111.         return copyOnWriteArrayList.indexOf(o);
  112.     }
  113.  
  114.     /** Returns true if this list contains no elements. **/
  115.     public boolean isEmpty()
  116.     {
  117.         return copyOnWriteArrayList.isEmpty();
  118.     }
  119.  
  120.     /** Returns an iterator over the elements in this list in proper sequence. **/
  121.     public Iterator<E> iterator()
  122.     {
  123.         return copyOnWriteArrayList.iterator();
  124.     }
  125.  
  126.     /**
  127.      * Returns the index of the last occurrence of the specified element in this
  128.      * list, or -1 if this list does not contain the element.
  129.     **/
  130.     public int lastIndexOf(Object o)
  131.     {
  132.         return copyOnWriteArrayList.lastIndexOf(o);
  133.     }
  134.  
  135.     /**
  136.      * Returns a list iterator over the elements in this list (in proper
  137.      * sequence).
  138.     **/
  139.     public ListIterator<E> listIterator()
  140.     {
  141.         return copyOnWriteArrayList.listIterator();
  142.     }
  143.  
  144.     /**
  145.      * Returns a list iterator over the elements in this list (in proper
  146.      * sequence), starting at the specified position in the list.
  147.     **/
  148.     public ListIterator<E> listIterator(int index)
  149.     {
  150.         return copyOnWriteArrayList.listIterator(index);
  151.     }
  152.  
  153.     /** Removes the element at the specified position in this list. **/
  154.     public E remove(int index)
  155.     {
  156.         return copyOnWriteArrayList.remove(index);
  157.     }
  158.  
  159.     /**
  160.      * Removes the first occurrence of the specified element from this list, if
  161.      * it is present.
  162.     **/
  163.     public boolean remove(Object o)
  164.     {
  165.         return copyOnWriteArrayList.remove(o);
  166.     }
  167.  
  168.     /**
  169.      * Removes from this list all of its elements that are contained in the
  170.      * specified collection.
  171.     **/
  172.     public boolean removeAll(Collection<?> c)
  173.     {
  174.         return copyOnWriteArrayList.removeAll(c);
  175.     }
  176.  
  177.     /**
  178.      * Retains only the elements in this list that are contained in the
  179.      * specified collection.
  180.     **/
  181.     public boolean retainAll(Collection<?> c)
  182.     {
  183.         return copyOnWriteArrayList.removeAll(c);
  184.     }
  185.  
  186.     /**
  187.      * Replaces the element at the specified position in this list with the
  188.      * specified element.
  189.     **/
  190.     public E set(int index, E element)
  191.     { 
  192.         return copyOnWriteArrayList.set(index, element);
  193.     }
  194.  
  195.     /** Returns the number of elements in this list. **/
  196.     public int size()
  197.     {
  198.         return copyOnWriteArrayList.size();
  199.     }
  200.  
  201.     /**
  202.      * Returns a view of the portion of this list between the specified
  203.      * fromIndex, inclusive, and toIndex, exclusive.
  204.     **/
  205.     public List<E> subList(int fromIndex, int toIndex)
  206.     {
  207.         return copyOnWriteArrayList.subList(fromIndex, toIndex);
  208.     }
  209.  
  210.     /**
  211.      * Returns an array containing all of the elements in this list in proper
  212.      * sequence (from first to last element).
  213.     **/
  214.     public Object[] toArray()
  215.     {
  216.         return copyOnWriteArrayList.toArray();
  217.     }
  218.  
  219.     /**
  220.      * Returns an array containing all of the elements in this list in proper
  221.      * sequence (from first to last element); the runtime type of the returned
  222.      * array is that of the specified array.
  223.     **/
  224.     public <T> T[] toArray(T[] a)
  225.     {
  226.         return copyOnWriteArrayList.toArray(a);
  227.     }
  228.  
  229.     public static void main(String...arg)
  230.     {
  231.         CopyOnWriteArrayListImpl<Integer> copyOnWriteArrayList = new CopyOnWriteArrayListImpl<Integer>();
  232.         copyOnWriteArrayList.add(10);
  233.         copyOnWriteArrayList.add(20);
  234.         copyOnWriteArrayList.add(30);
  235.         copyOnWriteArrayList.add(3, 40);
  236.         copyOnWriteArrayList.add(-10);
  237.  
  238.         Set<Integer> addAll = new HashSet<Integer>();
  239.         addAll.add(101);
  240.         addAll.add(200);
  241.         addAll.add(300);
  242.         copyOnWriteArrayList.addAll(addAll);
  243.  
  244.         Set<Integer> indexAddAll = new HashSet<Integer>();
  245.         indexAddAll.add(101);
  246.         indexAddAll.add(102);
  247.         indexAddAll.add(103);
  248.         copyOnWriteArrayList.addAll(5, indexAddAll);
  249.  
  250.         if (copyOnWriteArrayList.contains(101))
  251.             System.out.println("the copyOnWriteArrayList contains 101");
  252.         else
  253.             System.out.println("the copyOnWriteArrayList does not contain 101");
  254.  
  255.         System.out.println("the copyOnWriteArrayList contains " + copyOnWriteArrayList.get(5)
  256.             + " at index 5");
  257.         System.out.println("the index of 101 is " + copyOnWriteArrayList.indexOf(101));
  258.         System.out.println("the elements of copyOnWriteArrayList are");
  259.         Iterator<Integer> itr = copyOnWriteArrayList.iterator();
  260.         while (itr.hasNext())
  261.         {
  262.             System.out.print(itr.next() + "\t");
  263.         }
  264.         System.out.println();
  265.  
  266.         System.out.println("the last index of 101 is " + copyOnWriteArrayList.lastIndexOf(101));
  267.         System.out.println("elements from index 3 are");
  268.         ListIterator<Integer> listIterator = copyOnWriteArrayList.listIterator(3);
  269.         while (listIterator.hasNext())
  270.         {
  271.             System.out.print(listIterator.next() + "\t");
  272.         }
  273.         System.out.println();
  274.  
  275.         // removes element at index 5
  276.         copyOnWriteArrayList.remove(5);
  277.         System.out.println("elements removed and retained");
  278.         Set<Integer> removeAll = new HashSet<Integer>();
  279.         removeAll.add(30);
  280.         removeAll.add(300);
  281.         removeAll.add(101);
  282.         copyOnWriteArrayList.removeAll(removeAll);
  283.  
  284.         Set<Integer> retainAll = new HashSet<Integer>();
  285.         retainAll.add(10);
  286.         retainAll.add(20);
  287.         retainAll.add(200);
  288.         retainAll.add(-10);
  289.         retainAll.addAll(addAll);
  290.  
  291.         copyOnWriteArrayList.retainAll(retainAll);
  292.         copyOnWriteArrayList.set(1, 101);
  293.         System.out.println("the size of the copyOnWriteArrayList is " + copyOnWriteArrayList.size());
  294.         System.out.println("the elements of the returned list are");
  295.         List<Integer> list = copyOnWriteArrayList.subList(0,1);
  296.         for (int i = 0; i < list.size(); i++)
  297.         {
  298.             System.out.println(list.get(i) + "\t");
  299.         }
  300.         System.out.println();
  301.         System.out.println("the elements of returned array are");
  302.         Object[] array = (Object[]) copyOnWriteArrayList.toArray();
  303.         for (int i = 0; i < array.length; i++)
  304.         {
  305.             System.out.print(array[i] + "\t");
  306.         }
  307.         System.out.println();
  308.         System.out.println("the size of the copyOnWriteArrayList is " + copyOnWriteArrayList.size());
  309.         copyOnWriteArrayList.clear();
  310.         if (copyOnWriteArrayList.isEmpty())
  311.             System.out.println("the copyOnWriteArrayList is empty");
  312.         else
  313.             System.out.println("the copyOnWriteArrayList is not empty");
  314.     }
  315. }

$ javac CopyOnWriteArrayListImpl.java
$ java CopyOnWirteArrayListImpl
the copyOnWriteArrayList contains 101
the copyOnWriteArrayList contains 102 at index 5
the index of 101 is 7
the elements of copyOnWriteArrayList are
10	20	30	40	-10	102	103	101	101	200	300	
the last index of 101 is 8
elements from index 3 are
40	-10	102	103	101	101	200	300	
elements removed and retained
the size of the copyOnWriteArrayList is 2
the elements of the returned list are
40	
 
the elements of returned array are
40	101	
the size of the copyOnWriteArrayList is 2
the copyOnWriteArrayList is empty

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.