Why iterator is fail safe
HashMap ; import java. Iterator ; import java. ConcurrentModificationException at java. ConcurrentHashMap ; import java. ConcurrentHashMap; import java. Reinforcement Learning. R Programming. React Native. Python Design Patterns. Python Pillow. Python Turtle. Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics.
Ethical Hacking. Computer Graphics. Software Engineering. So first let us see the key differences between the fail-fast and fail-safe iterator in Java and then we'll see some Java programs to explain those features. 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.
Note that not all the iterators support all these methods. As exp. ListIterator supports add method but the general iterator doesn't. With fail-safe iterator element-changing operations on iterators themselves remove, set, and add are not supported.
These methods throw UnsupportedOperationException. Now let us see some detailed explanation and supporting programs to see these features of both fail-fast and fail-safe iterators. An iterator is considered fail-fast if it throws a ConcurrentModificationException under either of the following two conditions:.
The iterator will throw a ConcurrentModificationException if the underlying collection is structurally modified in any way except through the iterator's own remove or add if applicable as in ListIterator methods. Instead, it has semantics that is described by the official specification as weakly consistent memory consistency properties in Java. ConcurrentHashMap; import java.
This means that this iterator can tolerate concurrent modification, traverses elements as they existed when iterator was constructed and may but not guaranteed to reflect modifications to the collection after the construction of the iterator. Skip to content. Change Language. Related Articles. Table of Contents. Save Article.
Improve Article. Like Article. Next ConcurrentHashMap in Java. Recommended Articles.
0コメント