CopyOnWriteArrayList listIterator method

The CopyOnWriteArrayList listIterator method returns a list iterator over the elements in the sequence they were inserted.

As explained in the previous posts, CopyOnWriteArrayList creates its copy when performing any element changing operations, hence the list iterator returned in the CopyOnWriteArrayList listIterator method is the snapshot of the state of the list when the iterator was constructed.

CopyOnWriteArrayList supports 2 listIterator methods
1. public ListIterator listIterator()
Returns a list iterator over the elements in this list (in proper sequence).

2. public ListIterator listIterator(int index)
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

remove/add/set methods are not supported by the list iterator. An java.lang.UnsupportedOperationException is thrown if any of these methods are called on the list iterator.

In the example below we will see how to traverse the list iterator and also see what happens when we try to use the add method on the list iterator.

Sample Code


CopyOnWriteArrayList listIterator


1. CopyOnWriteArrayList listIterator java docs