we can do remove operation in ArrayList while iterating then Why we can't do the same operation CopyOnWriteArrayList while iterating?
why UnsupportedOperationException occurs during this process?
we can do remove operation in ArrayList while iterating then Why we can't do the same operation CopyOnWriteArrayList while iterating?
why UnsupportedOperationException occurs during this process?
All mutators (add, remove etc...) of CopyOnWriteArrayList are just creating new array and iterator() just create an Iterator with a snapshot of the array to exclude the possibility of interference, the docs:
The "snapshot" style iterator method 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.