Question

I get this definition : As name suggest fail-fast Iterators fail as soon as they realized that structure of Collection has been changed since iteration has begun.

what it mean by since iteration has begun? is that mean after Iterator it=set.iterator() this line of code?

public static void customize(BufferedReader br) throws IOException{  
    Set<String> set=new HashSet<String>(); // Actual type parameter added  
    **Iterator it=set.iterator();**
Was it helpful?

Solution

First of all, they are fail-fast, not fail-safe.

The contract is that structural modifications (i.e. insertions/deletions) of certain types of collections invalidate existing iterators into the collection. Fail-fast iterators attempt to detect that they are not supposed to be valid and throw a ConcurrentModificationException. This is done as a service to you, the programmer, to help discover this type of bugs quicker.

In your example:

Iterator it = set.iterator();
it.next();
set.add("unique-entry"); // invalidates the iterator
it.next();

If you're lucky, the second it.next() will detect the invalid usage and throw an exception. Note that this is done on a best-effort basis and is not guaranteed.

OTHER TIPS

is that mean after Iterator it=set.iterator() this line of code?

Yes. If you look at the code for HashSet.iterator() you'll see it's just this:

return map.keySet().iterator();

... which delegate's to HashMap.KeySet.iterator(). There are a few more links in the chain, but eventually you get to HashMap.HashIterator, which contains this in the constructor:

private abstract class HashIterator<E> implements Iterator<E> {
    int expectedModCount;   // For fast-fail

    ...

    HashIterator() {
        expectedModCount = modCount;
        ...
    }
}

... where modCount is a field in the enclosing instance of HashMap which keeps track of the number of modifications.

The iterator being fail-fast means the following piece of code is expected to fail:

Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
set.add("");
it.next();   // the set has changed now, and the iterator will throw an exception

because the following series of events occur: The iterator is created, then its underlying collection changes, then the iterator is accessed.

yes, don't change the collection after using .iterator() if you are planning to iterate over it , you can use the .remove() if you want to remove the latest element though

Before Fail Fast Iterator is starting to work it’s getting count of collection and after any iteration it is checking if count is changed or not, and in case of changed count JVM will throw ConcurrentModificationException. Fail fast iterators are any iterator of collection which is inside java.util package(e.g. ArrayList, LinkedList etc) and Fail Safe iterators are iterators which are inside of java.concurrent package(e.g. CopyOnWriteArrayList, CopyOnWriteSet etc.). Fail Fast iterators will throw exception in case of concurrent modification but Fail Safe iterator is basically working with copy of collection which is not throwing exception in case of concurrent modification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top