Monday, 10 August 2015

Iterator in Java

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element.
The easiest way to do this is to employ an iterator, which is an object that implements either the Iterator or the ListIterator interface.
Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.
Methods of Iterator:
§  hasNext()
§  next()
§  remove()

Methods of ListIterator:
§  add(E e)
§  hasNext()
§  hasPrevious()
§  next()
§  nextIndex()
§  previous()
§  previousIndex()
§  remove()
§  set(E e)

Difference between Iterator and ListIterator in java

1) Iterator is used for traversing List and Set both. We can use ListIterator to traverse List only, we cannot traverse Set using ListIterator.
2) We can traverse in only forward direction using Iterator.Using ListIterator, we can traverse a List in both the directions (forward and Backward).
3) We cannot obtain indexes while using Iterator We can obtain indexes at any point of time while traversing a list using ListIterator. The methods nextIndex() and previousIndex() are used for this purpose.
4) We cannot add element to collection while traversing it using Iterator, it throws ConcurrentModificationException when you try to do it. We can add element at any point of time while traversing a list using ListIterator.
5) We cannot replace the existing element value when using Iterator. By using set(E e) method of ListIterator we can replace the last element returned by next() or previous() methods.
There are two types of iterator based on Concurrent Modification.
Fail fast Iterator

Fail fast iterator while iterating through the collection , instantly throws Concurrent Modification Exception if there is structural modification  of the collection . According to  
Oracle docs , the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. ArrayList source code:

Fail Safe Iterator :

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. 
CopyOnWriteArrayList source


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. 

This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads. The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException.

Kindly provide your valuable feedback in comment box.

No comments:

Post a Comment