Skip to main content

Posts

Showing posts with the label Iterator

Fail Fast and Fail Safe Iterator in Java

  The concepts of "fail-safe" and "fail-fast" are often discussed in the context of data structures and algorithms, particularly in Java programming. Let's understand what each concept means: Fail-Safe: Fail-safe refers to the property of a system or data structure that guarantees it will not fail, crash, or produce incorrect results, even when it encounters unexpected inputs or concurrent modifications. In the context of Java collections, a fail-safe iterator is an iterator that does not throw a ConcurrentModificationException if the collection is modified while it's being iterated. Instead, it operates on a copy of the original collection or uses some mechanism to handle concurrent modifications safely. For example, the Iterator returned by HashMap or ConcurrentHashMap in Java is fail-safe. If a collection is modified while an iterator is traversing it, the iterator will continue to operate on the original elements that were present when the iteration ...