CopyOnWriteArrayList in Java

CopyOnWriteCopyOnWriteArrayList & its feature ::1. CopyOnWriteCopyOnWriteArrayList class is another important class of Collection framework.2. CopyOnWriteArrayList was intoduced in java 1.5 version inside java.util.concurrent package.3. CopyOnWriteArrayList implements generic List, RandomAccess, Cloneable and java.io.Serializable4. CopyOnWriteArrayList is a thread-safe variant of java.util.ArrayList in which all mutative operations add(), remove() etc. are implemented by making a fresh copy of the underlying array.5. CopyOnWriteArrayList can accept any element including multiple null values.6. CopyOnWrite means, a copy of the whole list is made each time you write to the list such as adding an element or remove an element.So, CopyOnWriteArrayList is ordinarily too costly. But if you have very less add() or remove() method and more traversal in your requirement you can use thread safe conurrency utility CopyOnWriteArrayList.7. CopyOnWriteArrayList uses Iterator interface to traverse the elements.While traversing the elements using iterator if the structure of the CopyOnWriteArrayList is modified using add or remove method ,iterator does not throw ConcurrentModificationException.So iterators for CopyOnWriteArrayList are fail-safe.8. CopyOnWriteArrayList throws UnsupportedOperationException if iterator.remove() method is called while traversing the CopyOnWriteArrayList using iterator.9. The “snapshot” style iterator method for CopyOnWriteArrayList uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException.The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators such as remove() is not supported. This methods throws UnsupportedOperationException.Program ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

packagecom.ai1tutorial.collection.arraylist;

import java.util.Iterator;

import java.util.List;

import java.util.concurrent.CopyOnWriteArrayList;

publicclassArrayListDemo{

publicstaticvoidmain(String[]args){

List<String>copyOnWriteArrayList=newCopyOnWriteArrayList<String>();

copyOnWriteArrayList.add("Ajay");

copyOnWriteArrayList.add("Kumar");

copyOnWriteArrayList.add("Mohanty");

Iterator iterator=copyOnWriteArrayList.iterator();

System.out.println("Before List Modification::");

while(iterator.hasNext()){

copyOnWriteArrayList.add("Jyoti");

System.out.println(iterator.next());

copyOnWriteArrayList.remove("Ajay");

}

System.out.println("\nAfter List Modification::");

iterator=copyOnWriteArrayList.iterator();

while(iterator.hasNext()){

System.out.println(iterator.next());

}

}

}

Output ::

1

2

3

4

5

6

7

8

9

10

11

Before List Modification::

Ajay

Kumar

Mohanty

After List Modification::

Kumar

Mohanty

Jyoti

Jyoti

Jyoti

Example of UnsupportedOperationException on CopyOnWriteArrayList ::

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

packagecom.ai1tutorial.collection.arraylist;

import java.util.Iterator;

import java.util.List;

import java.util.concurrent.CopyOnWriteArrayList;

publicclassCopyOnWriteArrayListDemo{

publicstaticvoidmain(String[]args){

List<String>copyOnWriteArrayList=newCopyOnWriteArrayList<String>();

copyOnWriteArrayList.add("Ajay");

copyOnWriteArrayList.add("Kumar");

copyOnWriteArrayList.add("Mohanty");

Iterator iterator=copyOnWriteArrayList.iterator();

System.out.println("Before List Modification::");

while(iterator.hasNext()){

System.out.println(iterator.next());

iterator.remove();

}

}

}

Output ::

1

2

3

4

5

Before List Modification::

Ajay

Exception inthread"main"java.lang.UnsupportedOperationException

at java.util.concurrent.CopyOnWriteArrayList$COWIterator.remove(Unknown Source)

at com.ai1tutorial.collection.arraylist.CopyOnWriteArrayListDemo.main(CopyOnWriteArrayListDemo.java:19)

About the Author

Dear Friends,
I am A.K.M. .This online journal ai1tutorial.com is a humble effort from my side to all students and professionals who want to learn computer science related technologies.I am not a professional blogger and I write posts in this blog only in my leisure time.I may not be 100% perfect in all my posts,but your review comments can surely help me to make it 100% perfect.