Tuesday, April 28, 2015

Enumeration vs Iterator vs ListIterator

I found a good article describe the differences in a good description with summary:

Iterator , ListIterator and Enumeration interfaces are mainly used to iterate collection classes .

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(); }

http://java2db.com/java-util/difference-between-iterator-listiterator-and-enumeration-in-java

No comments:

Post a Comment

java - fill distinct objects in ArrayList

If you have a list that contains some objects that it is considered as duplication when two or three fields of these objects are equal. How ...