Consider an ArrayList,where in for Iterator and List iterator operations,when a list is iterated and whenever there is a change in Collection object,then it throws ConcurrentModificationException like below :
package JavaImpPrograms;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Wdfds {
public static void main(String[] args) {
List<Integer> list=new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Iterator it=list.iterator();
while(it.hasNext()){
Integer i= (Integer) it.next();
if(i%2==0)
list.remove(0);
}
System.out.println(list); } }
This is not the case when Iterator object is updated i.e. like below:
while(it.hasNext()){
Integer i= (Integer) it.next();
if(i%2==0)
it.remove();
}
System.out.println(list); } }
And when it comes to copyOnWriteArrayList , if iterator object is updated with remove operation for normal iterator like below (or) listIterator is updated(either add/removal), it throws UnsupportedOperationException:
package JavaImpPrograms;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
public class Wdfds {
public static void main(String[] args) {
List<Integer> list=new CopyOnWriteArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
Iterator it=list.iterator();
while(it.hasNext()){
Integer i= (Integer) it.next();
if(i%2==0)
it.remove();
}
System.out.println(list); } }
package JavaImpPrograms;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class Wdfds {
public static void main(String[] args) {
List<Integer> list=new CopyOnWriteArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
ListIterator it=list.listIterator();
while(it.hasNext()){
Integer i= (Integer) it.next();
if(i%2==0)
it.add(9);
}
System.out.println(list); } }
I have a couple of Questions with the above results:
1) For an ArrayList,if an iterator is able to modify the list during iteration using iterator object, why is copyOnWriteArrayList used for?
1)Why copyOnWriteArrayList iterator updates are throwing unsupportedOperationExceptions when it encounters Iterator object changes, but no exception when there is a change for collection object?
3)Looks like above 2 scenarios are opposite to each other.Please let me know when these are used for and in which scenarios?
It is totally Confusing...
Your questions are all answered by the documentation of
CopyOnWriteArrayList:Looking at the documentation of
CopyOnWriteArrayList#iterator()reveals thatThe important part is that the
Iteratoronly provides a snapshot of the list.Iterator#remove()demands the following behaviour:Since the
iterator()of aCopyOnWriteArrayListis only a snapshot of the list, some element seen by theIteratorcould already been deleted from the list, thus removing it (again) might cause trouble (e.g. when the same object is in the list multiple times). The only logical consequence is to deny the operation by throwing anUnsupportedOperationException.