CopyOnWriteArrayList vs Array List in Java
CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations.
Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every writes operation but it's very efficient if you have a List where Iteration outnumbers mutation e.g. you mostly need to iterate the ArrayList and don't modify it too often.
Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException even if underlying CopyOnWriteArrayList is modified once Iteration begins because Iterator is operating on a separate copy of ArrayList. Consequently, all the updates made on CopyOnWriteArrayList is not available to Iterator (see Java Fundamentals: Collections).
In this Java Collection tutorial we will see What is CopyOnWriteArrayList in Java, Difference between ArrayList and CopyOnWriteArrayList in Java and One simple Java program example on How to use CopyOnWriteArrayList in Java.
CopyOnWriteArrayList Example in Java
CopyOnWriteArrayList Example in Java - Difference with ArrayListHere is a complete code Example of CopyOnWriteArrayList which demonstrate that Iterator of CopyOnWriteArrayList doesn't support remove() operation.
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
/**
public class CopyOnWriteArrayListExample{
public static void main(String args[]) {
CopyOnWriteArrayList<String> threadSafeList = new CopyOnWriteArrayList<String>();
threadSafeList.add("Java");
threadSafeList.add("J2EE");
threadSafeList.add("Collection");
//add, remove operator is not supported by CopyOnWriteArrayList iterator
Iterator<String> failSafeIterator = threadSafeList.iterator();
while(failSafeIterator.hasNext()){
System.out.printf("Read from CopyOnWriteArrayList : %s %n", failSafeIterator.next());
failSafeIterator.remove(); //not supported in CopyOnWriteArrayList in Java
}
}
}
Output:
Read from CopyOnWriteArrayList : Java
Read from CopyOnWriteArrayList : J2EE
Read from CopyOnWriteArrayList : Collection
If we uncomment, commented code in this Java program which modifies CopyOnWriteArrayList using Iterator then we will get following Exception :
Read from CopyOnWriteArrayList : Java
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove(CopyOnWriteArrayList.java:1004)
at test.CollectionTest.main(CollectionTest.java:29)
Java Result: 1
Here is a summary of CopyOnWriteArrayList in a slide, it teaches you what all the words have taught you so far:
What is CopyOnWriteArrayList in Java
That's all about what is CopyOnWriteArrayList, the difference between CopyOnWriteArrayList and ArrayList in Java and an example of CopyOnWriteArrayList. In short, use CopyOnWriteArrayList if you mostly require to Iterate over list without modifying it.
difference between ArrayList and CopyOnWriteArrayList is that Iterator of ArrayList is fail-fast and throw ConcurrentModificationException once detect any modification in List once iteration begins but Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException.
but is throws an exception while you are using CopyOnWriteArrayList
CopyOnWriteArrayList itself supports the remove method. However, the iterator to a CopyOnWriteArrayList does not support the remove() method and consequently throws the UnsupportedOperationException.
CopyOnWriteArrayList is a concurrent Collection class introduced in Java 5 Concurrency API along with its popular cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements List interface like ArrayList, Vector, and LinkedList but its a thread-safe collection and it achieves its thread-safety in a slightly different way than Vector or other thread-safe collection class. As the name suggests CopyOnWriteArrayList creates a copy of underlying ArrayList with every mutation operation e.g. add, remove, or when you set values. That's why it is only suitable for a small list of values which are read frequently but modified rarely e.g. a list of configurations.
Normally CopyOnWriteArrayList is very expensive because it involves costly Array copy with every writes operation but it's very efficient if you have a List where Iteration outnumbers mutation e.g. you mostly need to iterate the ArrayList and don't modify it too often.
Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException even if underlying CopyOnWriteArrayList is modified once Iteration begins because Iterator is operating on a separate copy of ArrayList. Consequently, all the updates made on CopyOnWriteArrayList is not available to Iterator (see Java Fundamentals: Collections).
In this Java Collection tutorial we will see What is CopyOnWriteArrayList in Java, Difference between ArrayList and CopyOnWriteArrayList in Java and One simple Java program example on How to use CopyOnWriteArrayList in Java.
CopyOnWriteArrayList Example in Java
CopyOnWriteArrayList Example in Java - Difference with ArrayListHere is a complete code Example of CopyOnWriteArrayList which demonstrate that Iterator of CopyOnWriteArrayList doesn't support remove() operation.
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
/**
public class CopyOnWriteArrayListExample{
public static void main(String args[]) {
CopyOnWriteArrayList<String> threadSafeList = new CopyOnWriteArrayList<String>();
threadSafeList.add("Java");
threadSafeList.add("J2EE");
threadSafeList.add("Collection");
//add, remove operator is not supported by CopyOnWriteArrayList iterator
Iterator<String> failSafeIterator = threadSafeList.iterator();
while(failSafeIterator.hasNext()){
System.out.printf("Read from CopyOnWriteArrayList : %s %n", failSafeIterator.next());
failSafeIterator.remove(); //not supported in CopyOnWriteArrayList in Java
}
}
}
Output:
Read from CopyOnWriteArrayList : Java
Read from CopyOnWriteArrayList : J2EE
Read from CopyOnWriteArrayList : Collection
If we uncomment, commented code in this Java program which modifies CopyOnWriteArrayList using Iterator then we will get following Exception :
Read from CopyOnWriteArrayList : Java
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove(CopyOnWriteArrayList.java:1004)
at test.CollectionTest.main(CollectionTest.java:29)
Java Result: 1
Here is a summary of CopyOnWriteArrayList in a slide, it teaches you what all the words have taught you so far:
What is CopyOnWriteArrayList in Java
That's all about what is CopyOnWriteArrayList, the difference between CopyOnWriteArrayList and ArrayList in Java and an example of CopyOnWriteArrayList. In short, use CopyOnWriteArrayList if you mostly require to Iterate over list without modifying it.
difference between ArrayList and CopyOnWriteArrayList is that Iterator of ArrayList is fail-fast and throw ConcurrentModificationException once detect any modification in List once iteration begins but Iterator of CopyOnWriteArrayList is fail-safe and doesn't throw ConcurrentModificationException.
but is throws an exception while you are using CopyOnWriteArrayList
CopyOnWriteArrayList itself supports the remove method. However, the iterator to a CopyOnWriteArrayList does not support the remove() method and consequently throws the UnsupportedOperationException.