CopyOnWriteArrayList iterator method

The CopyOnWriteArrayList iterator method returns an 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 iterator returned in the CopyOnWriteArrayList iterator method is the snapshot of the state of the list when the iterator was constructed.

remove method is not supported by the iterator. An java.lang.UnsupportedOperationException is thrown if remove() is called on the iterator.

In the below code we will see how to iterate over the CopyOnWriteArrayList, and also see what happens when we call the remove() on the iterator.

Sample Code

Output

CopyOnWriteArrayList iterator method

References

1. CopyOnWriteArrayList iterator method java docs