6 Difference between ArrayList and CopyOnWriteArrayList in Java with Example

I have already shared how ArrayList add() method works internally in java.  In this post we will learn about the difference between ArrayList and CopyOnWriteArrayList. ArrayList is one of the most frequently used collection class besides HashMap . So let us find out the difference between ArrayList and CopyOnWriteArrayList.


Read Also :  Array vs ArrayList in Java



Difference between ArrayList and CopyOnWriteArrayList in Java

1. Synchronization :  ArrayList is not synchronized . CopyOnWriteArrayList is synchronized . Synchronization means at a time only one thread can access the object.

2. Performance :  ArrayList is faster as it is not synchronized. That means many threads can execute the same piece of code simultaneously. In comparison ,  CopyOnWriteArrayList is slower.


3. Fail-fast vs Fail-safe : Iterators returned by ArrayList's iterator and listiterator  methods are fail-fast. CopyOnWriteArrayList uses fail-safe iterator . We have already shared the fail-fast vs fail-safe iterators in java with example

4. ConcurrentModificationException : ArrayList can throw ConcurrentModificationException  while CopyOnWriteArrayList can not .

5. Added in java version : ArrayList class was added in java version 1.2 , while CopyOnWriteArrayList class was added in java version 1.5 (or java 5) .

6. Package :  ArrayList class is present in java.util package , while CopyOnWriteArrayList class is present in java.util.concurrent package.


Example of ArrayList and CopyOnWriteArrayList in Java


import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;
public class ArrayListCopyOnWriteArrayListExample { public static void main(String[] args) { 
 
           
  
        ArrayList<String> arrobj = new ArrayList<String>();
        arrobj.add("Alive is awesome");
        arrobj.add("Love yourself");
        System.out.println("ArrayList object output :"+ arrobj);
 
         
 
        CopyOnWriteArrayList<String> coponwrtobj = 
                                           new CopyOnWriteArrayList<String>();
        coponwrtobj.add("Alive is awesome");  
        coponwrtobj.add("Love yourself"); 
        System.out.println("CopyOnWriteArrayList object output :"+ coponwrtobj);   
 
 }
}




Output :  ArrayList object output :[Alive is awesome, Love yourself]
                 CopyOnWriteArrayList object output :[Alive is awesome, Love yourself]
   


Similarities between ArrayList and CopyOnWriteArrayList

1. Permit null values : Both ArrayList and CopyOnWriteArrayList permits null.

2. Java Collections Framework : Both classes are part of the Java Collections Framework.

3. Clone method : Both classes clone method return the shallow copy of the original object.


When to prefer CopyOnWriteArrayList over ArrayList 

Difference between ArrayList and CopyOnWriteArrayList in Java
CopyOnWriteArrayList is 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.
So, if your code involves multi-threading tasks then prefer CopyOnWriteArrayList over ArrayList .
Otherwise , always use ArrayList .




Recap : Difference between ArrayList and CopyOnWriteArrayList in Java




ArrayListCopyOnWriteArrayList
SynchronizedNoYes
Thread-SafeNoYes
Throws ConcurrentModificationException Yes No
Iterator typeFail fast iteratorFail safe iterator
PerformanceFastSlow in comparision
Added Java 1.2 Java 1.5 





If you have any other questions or doubts regarding the difference between ArrayList and CopyOnWriteArrayList in Java , please mention in comments.




About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry