Differences and Similarities between ArrayList and CopyOnWriteArrayList with program in java


It’s very important to differentiate between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList, so in this Collection framework tutorial we will learn what are differences and similarities between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList in java.



Contents of page :
  • Differences between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList in java >
  • Similarity between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList in java   >
  • Program 1 to show
    • Iterator, listIterator and Enumeration  returned by java.util.ArrayList are Fail-fast in java
  • Program 2 to show
    • Iterator, listIterator and Enumeration  returned by java.util.concurrent.CopyOnWriteArrayList are Fail-safe in java


Differences between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList  in java >

Property
java.util.ArrayList
java.util.concurrent. CopyOnWriteArrayList
1
synchronization
ArrayList is not synchronized  (because 2 threads on same ArrayList object can access it at same time).

I have created program to show see consequence of using ArrayList in multithreading environment.
In the program i will implement our own arrayList.
CopyOnWriteArrayList  is synchronized  (because 2 threads on same CopyOnWriteArrayList object cannot access it at same time).
2
Iterator and listIterator
Iterator and listIterator returned by ArrayList are Fail-fast, means any structural modification made to ArrayList during iteration using Iterator or listIterator will throw ConcurrentModificationException in java.

As shown in Program 1 below.
Iterator and listIterator returned by CopyOnWriteArrayList are Fail-safe in java.


As shown in Program 2 below.


3
Enumeration is fail-fast
Enumeration returned by ArrayList is fail-fast, means any structural modification made to ArrayList during iteration using Enumeration will throw ConcurrentModificationException.


As shown in Program 1 below.
Enumeration returned by CopyOnWriteArrayList is fail-safe.







As shown in Program 2 below.
4
Iterate using enhanced for loop
Iteration done on ArrayList using enhanced for loop is Fail-fast, means any structural modification made to ArrayList during iteration using enhanced for loop will throw ConcurrentModificationException.

As shown in Program 1 below.
Iteration done on CopyOnWriteArrayList using enhanced for loop is Fail-safe.

As shown in Program 2 below.
5
Performance
ArrayList is not synchronized, hence its operations are faster as compared to CopyOnWriteArrayList.
CopyOnWriteArrayList is synchronized, hence its operations are slower as compared to ArrayList.
6
AbstractList
ArrayList extends AbstractList (abstract class) which provides implementation to  List interface to minimize the effort required to implement this interface backed by RandomAccess interface.
CopyOnWriteArrayList does not extends AbstractList, though CopyOnWriteArrayList also implements RandomAccess interface.
7
Introduced in which java version
ArrayList was introduced in second version of java (1.2) i.e. JDK 2.0
CopyOnWriteArrayList was introduced in fifth version of java (1.5) i.e. JDK 5.0
8
Package
java.util
java.util.concurrent



So far we have learned what are differences between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList in java.
Now we will learn similarities in java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList in Collection framework in java.




/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


Similarity between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList   >

Property
java.util.ArrayList and
java.util.concurrent.CopyOnWriteArrayList
1
Insertion order
ArrayList and
CopyOnWriteArrayList both maintains insertion order in java.
2
Allows null
ArrayList and
CopyOnWriteArrayList both allows to store null in java.
3
Implements java.util.List
ArrayList and
CopyOnWriteArrayList both are implementation of the java.util.List interface.
4
Structure
ArrayList and
CopyOnWriteArrayList both are index based structures in java.

5
How get(index) method works?
Get method directly gets element on specified index from ArrayList and
CopyOnWriteArrayList. Hence, offering O(1) complexity.
6
Resizable
ArrayList and
CopyOnWriteArrayList both are Resizable-array in java.
7
RandomAccess interface
ArrayList and
CopyOnWriteArrayList both implements RandomAccess(Marker interface) to indicate that they support fast random access (i.e. index based access)
8
Complexity offered by methods are same

Operation/ method
Worst case
Best case
add
O(n), when array is full it needs restructuring,
operation runs in amortized constant time.
O(1), when array does not need any restructuring.
remove
O(n), when removal is done from between restructuring is needed.
O(1), when removal is done at last position, no restructuring is needed.
get
O(1), it is index based structure. So, complexity of  get operation is always done in O(1).
O(1) it is index based structure. So, complexity of  get operation is always done in O(1).
set
O(1), it is index based structure, no restructuring is needed in set operation. So, complexity of operation is always O(1)
O(1), it is index based structure, no restructuring is needed in set operation. So, complexity of operation is always O(1)
iterator
O(n), because ireation is done over each and every element.
O(n), because ireation is done over each and every element.
listIterator
O(n), its same as iterator.
O(n), its same as iterator.
enumeration
O(n), its same as iterator.
O(n), its same as iterator.


9
Duplicate elements
ArrayList and
CopyOnWriteArrayList both allows to store duplicate elements in java.







Example/Program 1 to show
Iterator, listIterator and Enumeration  returned by ArrayList are Fail-fast, means any structural modification made to ArrayList during iteration will throw ConcurrentModificationException in java.
Also, iteration done on ArrayList using enhanced for loop is Fail-fast in java.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class ArrayListExample {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          List<String> arrayList = new ArrayList<String>();
          arrayList.add("ind");
          arrayList.add("america");
          // fail-fast
          Iterator<String> iterator = arrayList.iterator();
          while (iterator.hasNext()) {
                 arrayList.add("newEle"); // unComment to avoid ConcurrentModificationException
                 System.out.println(iterator.next());
          }
          // fail-fast
          ListIterator<String> listIterator = arrayList.listIterator();
          listIterator.next();
          listIterator.previous();
          while (listIterator.hasNext()) {
                 arrayList.add("newEle"); // unComment to avoid ConcurrentModificationException
                 System.out.println(listIterator.next());
          }
          // fail-fast
          Enumeration<String> listEnum = Collections.enumeration(arrayList);
          while (listEnum.hasMoreElements()) {
                 arrayList.add("newEle"); // unComment to avoid ConcurrentModificationException
                 System.out.println(listEnum.nextElement());
          }
 
          //enhanced for loop is fail-fast
          for (String string : arrayList) {
                 arrayList.add("newEle"); // unComment to avoid ConcurrentModificationException
                 System.out.println(string);
          }

   }
}
In above program structural modification was made to ArrayList during iteration, and that throwed ConcurrentModificationException.


Example/Program 2 to show
Iterator, listIterator and Enumeration  returned by CopyOnWriteArrayList are Fail-safe, means any structural modification made to CopyOnWriteArrayList during iteration does not throw any exception.
Also, iteration done on CopyOnWriteArrayList using enhanced for loop is Fail-safe in java.
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class CopyOnWriteArrayListExample {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          List<String> copyOnWriteArrayList = new CopyOnWriteArrayList<String>();
          copyOnWriteArrayList.add("ind");
          copyOnWriteArrayList.add("america");
          // fail-safe
          Iterator<String> iterator = copyOnWriteArrayList.iterator();
          while (iterator.hasNext()) {
                 copyOnWriteArrayList.add("newEle1");
                 System.out.println(iterator.next());
          }
          for (String string : copyOnWriteArrayList) {
                 System.out.println(string);
          }
          // fail-safe
          ListIterator<String> listIterator = copyOnWriteArrayList.listIterator();
          listIterator.next();
          listIterator.previous();
          while (listIterator.hasNext()) {
                 copyOnWriteArrayList.add("newEle2");
                 System.out.println(listIterator.next());
          }
          // fail-safe
          Enumeration<String> listEnum = Collections
                       .enumeration(copyOnWriteArrayList);
          while (listEnum.hasMoreElements()) {
                 copyOnWriteArrayList.add("newEle3");
                 System.out.println(listEnum.nextElement());
          }
 
          //enhanced for loop is fail-safe
          for (String string : copyOnWriteArrayList) {
                 copyOnWriteArrayList.add("newEle");
                 System.out.println(string);
          }

   }
}
In above program structural modification was made to CopyOnWriteArrayList during iteration, but that didn’t throw ConcurrentModificationException in java.


So in this Collection framework tutorial we learned what are differences and similarities between java.util.ArrayList and java.util.concurrent.CopyOnWriteArrayList in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Collection - List, Set and Map all properties in tabular form in java


List hierarchy tutorial in java - Detailed - java.util.ArrayList, java.util.LinkedList, java.util.vector, java.util.concurrent.CopyOnWriteArrayList classes



Important Similarity and Differences in java >

List Differences in java >

ArrayList vs LinkedList - Similarity and Differences in java


ArrayList vs Vector - Similarity and Differences in java


List vs Set - Similarity and Differences in java


Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >

HashSet vs CopyOnWriteArraySet in java - Similarity and Differences with program


TreeSet vs ConcurrentSkipListSet in java - Similarity and Differences with program


TreeMap vs ConcurrentSkipListMap in java - Similarity and Differences with program



No comments:

Post a Comment