java2db

Difference between Iterator and ListIterator in java



Iterator and ListIterator interfaces are mainly used to iterate collections.ListIterator is sub interface of Iterator.Main features and differences are given below.

Iterator

Iterator and ListIterator Iterator can iterate both Set , List interfaces and it’s implementations .
Iterator and ListIterator Reverse iteration is not possible with the Iterator .
Iterator and ListIterator Only element deletion is possible with the remove method .

ListIterator

Iterator and ListIterator ListIterator  iterates only List interface  and its implementations .
Iterator and ListIterator Both forward and backward (either direction)  iterations are possible .
Iterator and ListIterator Element addition , deletion and indexing is possible .

 

Methods in Iterator and ListIterator :

Iterator and ListIterator Iterator and ListIterator

Syntax:

 Iterator <name> = <Set / List object>.iterator();  ListIterator <name> = <List object>.listIterator();

Iterator and ListIterator example

Execution Result:

Iterator and ListIterator The functionality of Enumeration duplicated by the Iterator interface.




Related Posts :
Difference between Iterator ListIterator and Enumeration in java
   [will not be published]



^ <