fail-fast iterator Vs fail-safe iterator

fail-fast iterator fail-safe iterator
Is Modification of underlying Collection Allowed? NO. It throws ConcurrentModificationException if the underlying collection is structurally modified in any way except through the iterator’s own remove or add methods Note:.Fail-fast iterators throw ConcurrentModificationException on a best-effort basis and fail-fast behavior of an iterator cannot be guaranteed. YES. It doesn’t throw ConcurrentModificationException.
Whether works on Original Collection? YES NO.Iterator makes a copy of the underlying structure and iteration is done over that snapshot.So , original data structure remains  structurally unchanged .Hence , no ConcurrentModificationException is thrown by the fail safe iterator.
Will underlying collection’s mutations are reflected after the iterator was created? As iterator work on the original collection, iterator will reflect additions, removals, or changes to the collection since the iterator was created. Drawback of using a copy of the collection rather than original collection is that the iterator will not reflect additions, removals, or changes to the collection since the iterator was created.
Whether add, remove and set operations are provided? fail-fast iterator provides remove, set, and add operations. Element-changing operations on iterators themselves (remove(), set(), and add()) are not supported. These methods throw UnsupportedOperationException.
Which Collections are Supported? ArrayList, Vector, HashSet,HashMap ConcurrrentHashMap, CopyOnWriteArrayList, CopyOnWriteArraySet
Memory Overhead NO YES. The clone of the underlying collection needs to be created.

Leave a Reply

Your email address will not be published. Required fields are marked *