Is Iterator a class?
What is ConcurrentModificationException?
What is fail-fast property?
What is UnsupportedOperationException?
Difference b/w fail-fast & fail-safe iterators?
Concurrent Modification : Modification of an object concurrently when a task is already processing it
Fail-fast iterators
If a structural modification is detected then this iterator throws ConcurrentModificationException
Structural modification includes addition, removal or updating of elements while a collection is being iterated over.
Examples include Iterators on ArrayList, HashMap
Fail-safe iterators
Even if a structural modification is detected then this iterator does not throw an exception
Structural modification done on actual collection goes unnoticed by these iterators.
So while iterating up-to-date data is not guaranteed
Examples include Iterators on ConcurrentHashMap, CopyOnWriteArrayList
How fail-fast iterators work?
When an Iterator is created, it makes a copy of underlying structure
It then iterates over that snapshot
Any changes to the collection does not get reflected in the snapshot
So interference is not possible & it will never throw ConcurrentModificationException
remove, set and add operations on iterators are not supported. These methods throw UnsupportedOperationException.
fail-safe iterator & weakly consistent
In case of ConcurrentHashMap, it does not work on a separate copy although it is NOT fail-fast
It is called weakly-consistent
Weakly-consistent means it can tolerate concurrent modification while traversing and may reflect (but not guaranteed) concurrent modification