Difference between Iterator ListIterator and Enumeration in java

Iterator , ListIterator and Enumeration interfaces are mainly used to iterate collection classes . The main difference between Iterator ListIterator and Enumeration as follows.


iterator listiterator and enumeration


Enumeration :
java2db Enumeration  iterates Vector and Hashtable .
java2db Reverse iteration is not possible with enumeration.
java2db It cannot add or remove elements  while iteration .

Iterator :
java2db Iterator   iterates the  implementations of  List , Set .
java2db Reverse iteration is not possible  with Iterator .
java2db Iterator cannot add elements , but it can remove elements while iteration .

ListIterator :
java2dbListIterator  iterates  List  implementations (like  ArrayList , LinkedList  etc).
java2dbBoth forward and backward iterations are  possible with ListIterator .
java2dbBoth elements addition  and deletion can possible  with ListIterator .

Syntax for Iterator ListIterator and Enumeration :

Iterator  <Iterator variable  name>=  <set/List variable> . iterator();
while(<Iterator variable  name>.hasNext())  {
<Iterator variable  name> .next();               }
ListIterator <ListIterator variable name>=<list variable>.listIterstor();
while  (<ListIterator variable name>.hasNext() ){
<ListIterator variable name>.next(); }
Enumeration <Enumeration variable name>=<Vector / Hashtable variable>.elements();
while(<Enumeration variable name>.hasMoreElements()) {
<Enumeration variable name>.nextElement(); }

Example code for  Iterator ListIterator and Enumeration :

Execution Result:

Conclusion :

java2db The functionality of Enumeration is duplicated by the Iterator interface.
java2dbIn addition, Iterator adds an optional remove operation, and has shorter method names.
java2dbListIterator allows the programmers to traverse the list in either direction and also modification,possible  to get iterator’s current position in the list.

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

^ <