Friday 23 December 2016

Difference : Iterator vs ListIterator

Iterator and ListIterator differ based on following parameters:

1.      Traversal direction
Iterator allows to traverse the List, Set and Map in only forward direction. Iterator is unidirectional in nature.
ListIterator allows to traverse the List (remember only List) in both forward and backward direction. ListIterator is bidirectional.

2.      Method
Iterator has next() method, using which forward traversal is possible.
ListIterator has next() method for forward traversal and previous() method for backward traversal.

3.      Collection implementations that can be traversed
Iterator can traverse List, Set and Map.
ListIterator can only be used for List traversal.

4.      Modifying the elements while traversing
Iterator can only remove the element during iteration. It has only remove() method.
ListIterator can add, remove and set elements during iteration.

5.      Current position
Iterator cannot determine the current position of iteration during iterating over List.
ListIterator can determine the current position of iteration during iterating over List.

6.      Retrieve index
Index of the element cannot be retrieved using Iterator.
Index of the element can be retrieved using ListIterator using previousIndex() and nextIndex() method.

7.      Example

4 comments:

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.