Thursday, October 16, 2025
HomeLanguagesJavaDifference between ArrayList and CopyOnWriteArrayList

Difference between ArrayList and CopyOnWriteArrayList

Both ArrayList and CopyOnWriteArray implement List interface. But There are lots of differences between ArrayList and CopyOnWriteArrayList:

  • CopyOnWriteArrayList creates a Cloned copy of underlying ArrayList, for every update operation at certain point both will synchronized automatically which is takes care by JVM. Therefore there is no effect for threads which are performing read operation. Therefore thread-safety is not there in ArrayList whereas CopyOnWriteArrayList is thread-safe.
  • While Iterating ArrayList object by one thread if other thread try to do modification then we will get Runt-time exception saying ConcurrentModificationException. Where as We won’t get any Exception in the case of CopyOnWriteArrayList.
  • ArrayList is introduced in JDK 1.2 whereas CopyOnWriteArrayList is introduced by SUN people in JDK 1.5.
  • Iterator of ArrayList can perform remove operation while iteration. But Iterator of CopyOnWriteArrayList cant perform remove operation while iteration, otherwise it will throw run-time exception UnsupportedOperationException.

    Below is the implementation of this point.




// Java program to illustrate ArrayList
import java.util.*;
  
class CopyDemo
{
    public static void main(String[] args) 
    {
        ArrayList l = new ArrayList();
        l.add("A");
        l.add("B");
        l.add("C");
        Iterator itr = l.iterator();
          
        while (itr.hasNext()) 
        {
            String s = (String)itr.next();
              
            if (s.equals("B"))
            {
                // Can remove
                itr.remove();
            }
        }
        System.out.println(l);
    }
}


Output:

[A,C]




// Java program to illustrate CopyOnWriteArrayList
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
class CopyDemo extends Thread {
      
    static CopyOnWriteArrayList l = new CopyOnWriteArrayList();
      
    public static void main(String[] args) 
                throws InterruptedException
    {
        l.add("A");
        l.add("B");
        l.add("C");
        Iterator itr = l.iterator();
          
        while (itr.hasNext())
        {
            String s = (String)itr.next();
            System.out.println(s);
              
            if (s.equals("B"))
            {
                // Throws RuntimeException
                itr.remove();
            }
            Thread.sleep(1000);
        }
        System.out.println(l);
    }
}


Output:

A
B
Exception in thread "main" java.lang.UnsupportedOperationException
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11953 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS