The Wayback Machine - https://web.archive.org/web/20240717040602/https://www.geeksforgeeks.org/difference-arraylist-copyonwritearraylist/
Open In App

Difference between ArrayList and CopyOnWriteArrayList

Last Updated : 11 Dec, 2018
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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


Similar Reads

CopyOnWriteArrayList in Java
CopyOnWriteArrayList class is introduced in JDK 1.5, which implements the List interface. It is an enhanced version of ArrayList in which all modifications (add, set, remove, etc) are implemented by making a fresh copy. It is found in java.util.concurrent package. It is a data structure created to be used in a concurrent environment. Here are few p
6 min read
CopyOnWriteArrayList spliterator() method in Java
The spliterator() method of CopyOnWriteArrayList returns an spliterator over the elements in this list in proper sequence. There is no need of synchronization while operating on the spliterator. Syntax: public Spliterator spliterator() Parameters: The function does not accept any parameters. Return Value: The function returns an spliterator over th
2 min read
CopyOnWriteArrayList toString() method in Java
The toString() method of CopyOnWriteArrayList returns the string representation of the list. Syntax: public String toString() Parameters: The function does not accepts any parameter. Return Value: The function returns the string representation of the list. Below programs illustrate the above function: Program 1: // Java Program to illustrate the Co
1 min read
CopyOnWriteArrayList size() method in Java
The size() method of CopyOnWriteArrayList returns the size of the list. It returns the number of elements in the list. Syntax: public int size() Parameters: The function does not accepts any parameter. Return Value: The function returns the size of the list. Below programs illustrate the above function: Program 1: // Java Program to illustrate the
1 min read
CopyOnWriteArrayList listIterator() method in Java
The listIterator() method of CopyOnWriteArrayList returns an listIterator over the elements in this list in proper sequence. The listIterator does NOT support the remove, set or add methods. Syntax: public ListIterator listIterator() Parameters: The function does not accept any parameters. Return Value: The function returns a list iterator over the
3 min read
CopyOnWriteArrayList iterator() method in Java
The iterator() method of CopyOnWriteArrayList returns an iterator over the elements in this list in proper sequence. The iterator does NOT support the remove method. Syntax: public Iterator iterator() Parameters: The function does not accept any parameters. Return Value: The function returns an iterator over the elements in the list. Below programs
2 min read
CopyOnWriteArrayList lastIndexOf() method in Java
The lastIndexOf(Object o) method of CopyOnWriteArrayList returns the last occurrence of the element passed in the list. It returns -1 if the element is not present in the list. Syntax: public int lastIndexOf(Object o) Parameters: The function accepts a parameter o whose last occurrence is to be returned. Return Value: The function returns the last
3 min read
CopyOnWriteArrayList isEmpty() method in Java
The isEmpty(E e) method of CopyOnWriteArrayList checks if the list is empty or not. It returns true if the list is empty, else it returns false. Syntax: public boolean isEmpty() Parameters: The function does not accepts any parameter. Return Value: The function returns true if the list is empty, otherwise it returns false. Below programs illustrate
2 min read
CopyOnWriteArrayList indexOf() method in Java
The indexOf(Object o) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list. It returns -1 if the element is not present in the list. Syntax: public int indexOf(Object o) Parameters: The function accepts a parameter o whose first occurrence is to be returned. Return Value: The function returns the first occur
3 min read
CopyOnWriteArrayList hashCode() method in Java
The hashCode() method of CopyOnWriteArrayList returns the hashCode value of the list. Syntax: public int hashCode() Parameters: The function does not accepts any parameter. Return Value: The function returns the hashCode value of the list. Below programs illustrate the above function: Program 1: // Java Program to illustrate the CopyOnWriteArrayLis
1 min read
three90RightbarBannerImg