Friday, July 29, 2016

Fail Fast v/s Fail Safe Iterators in Collections



Iterators in java give us the facility to traverse over the Collection objects. Iterators returned by the Collection are either fail-fast in nature or fail-safe in nature.

 Fail-Fast iterators immediately throw ConcurrentModificationException if a collection is modified while iterating over it. Whereas Fail-Safe iterators don’t throw any exceptions if a collection is modified while iterating over it. Because, they operate on the clone of the collection, not on the actual collection.

Fail-Fast Iterators in Java:
Fail-Fast iterator, returned by most of the collection types, doesn’t tolerate any structural modifications to a collection while iterating over it. (Structural modifications means add, remove or updating an element in the collection) They throw ConcurrentModificationException if a collection is structurally modified while iteration is going on the collection. But, they don’t throw any exceptions if the collection is modified by the iterator’s own methods like remove().

The iterators returned by the ArrayList, Vector, HashMap etc. are all Fail-Fast in nature.

public class FailFastIteratorExample
{      
    public static void main(String[] args)
    {
        //Creating an ArrayList of integers
        ArrayList list = new ArrayList();
        //Adding elements to list
        list.add(1452);
        list.add(6854);
        list.add(8741);
        
        //Getting an Iterator from list
        Iterator it = list.iterator();
        while (it.hasNext())
        {
            Integer integer = (Integer) it.next();
            list.add(8457);      //This will throw ConcurrentModificationException
        }
    }   
}
  
Fail-Safe Iterators in Java:
Fail Safe Iterator makes copy of the internal data structure (object array) and iterates over the copied data structure. Any structural modification done to the iterator affects the copied data structure.  So, original data structure remains structurally unchanged .Hence, no ConcurrentModificationException throws by the fail safe iterator.

Two issues associated with Fail Safe Iterator are:

1. Overhead of maintaining the copied data structure i.e. memory.
2.  Fail safe iterator does not guarantee that the data being read is the data currently in the original data structure.

Iterator returned by ConcurrentHashMap is a fail-safe iterator.

public class FailSafeIteratorExample
{      
    public static void main(String[] args)
    {
        //Creating a ConcurrentHashMap
        ConcurrentHashMap map = new ConcurrentHashMap();
        //Adding elements to map
        map.put("ONE", 1);
        map.put("TWO", 2);
        map.put("THREE", 3);
        map.put("FOUR", 4);
        
        //Getting an Iterator from map
        Iterator it = map.keySet().iterator();
        while (it.hasNext())
        {
            String key = (String) it.next();
            System.out.println(key+" : "+map.get(key));
            map.put("FIVE", 5);     //This will not be reflected in the Iterator
        }
    }   

}

No comments:

Post a Comment