The Iterator and ListIterator are the two among the three cursors of Java. Both Iterator and ListIterator are defined by Collection Framework in Java.Util package. ListIterator is the child interface of Iterator interface. The major difference between Iterator and ListIterator is that Iterator can traverse the elements in the collection only in forward direction whereas, the ListIterator can traverse the elements in a collection in both the forward as well as the backwards direction.
Let us discuss some more differences between Iterator and ListIterator with the help of comparison chart shown below.
Content: Iterator Vs ListIterator
|Basis for comparison||Iterator||ListIterator|
|Basic||Iterator can traverse the elements in a collection only in forward direction.||ListIterator can traverse the elements in a collection in forward as well as the backwards direction.
|Add||Iterator is unable to add elements to a collection.||ListIteror can add elements to a collection.
|Modify||Iterator can not modify the elements in a collection.||ListIterator can modify the elements in a collection using set().|
|Traverse||Iterator can traverse Map, List and Set.||ListIterator can traverse List objects only.
|Index||Iterator has no method to obtain an index of the element in a collection.||Using ListIterator, you can obtain an index of the element in a collection.|
Definition of Iterator
Iterator is an interface in a Collection Framework. It is used to traverse through the collection elements. Iterator allows you to iterate each element in the collection one by one, obtain elements from the collection or remove elements from the collection. You can notice you can not modify any element in a collection using Iterator.
Iterator has a method iterator() that returns the iterator to the start of the collection. Once you get an iterator to the start of a collection then to traverse the elements in the collection establish a loop that calls hasNext() each time the loop iterates. The hasNext() if returns true it means that the next element is there in the collection, and if it returns false then it means all the elements are traversed. Then inside the loop, you can obtain each element in a Collection using next(). The next() method returns the next element of the collection.
Using Iterator you can traverse a collection in the forward direction only.
Using Iterator, you can not manipulate or modify the elements in a collection.
Definition of ListIterator
ListIterator is an interface in a Collection framework, and it extends the Iterator interface. Using ListIterator, you can traverse the elements of the collection in both forward and backwards directions. You can also add, remove or modify any element in the collection. In short, we can say it removes drawbacks of Iterator.
The methods of ListIterator are as follow:
hasNext(): If returns true confirms that there are more elements in a collection.
next(): Returns next elements of the list.
nextIndex(): Returns the index of next elements in the list.
hasPrevious(): Returns true if there are elements in the reverse direction in a collection.
previous(): Returns the previous element in a collection.
previousIndex(): Returns index of the previous element in a collection.
remove(): Delete the element from a collection.
set(): Modifies the element in a collection.
add(): Adds the new element in a collection.
Key Differences Between Iterator and ListIterator
- The basic difference between Iterator and ListIterator is that both being cursor, Iterator can traverse elements in a collection only in forward direction. On the other hand, the ListIterator can traverse in both forward and backward directions.
- Using iterator you can not add any element to a collection. But, by using ListIterator you can add elements to a collection.
- Using Iterator, you can not remove an element in a collection where, as You can remove an element from a collection using ListIterator.
- Using Iterator you can traverse all collections like Map, List, Set. But, by ListIteror you can traverse List implemented objects only.
- You can retrieve an index of an element using Iterator. But as List is sequential and index-based you can retrieve an index of an element in using ListIterator.
You can use ListIterator when you have to particularly traverse a List object in both forward and reverse direction. Else you can use Iterator as it supports all type collection objects.