Enumeration v/s Iterator v/s ListIterator interfaces

In this article, we will compare 3 important cursors Enumeration, Iterator & ListIterator in detail i.e.; Enumeration v/s Iterator v/s ListIterator interfaces

All 3 cursors are used to iterate over collection items, but there are certain differences between each one of them

 

So let’s us discuss in tabular format;

 

Enumeration v/s Iterator v/s ListIterator:

Enumeration Iterator ListIterator
This is part of Legacy collection introduced in Java 1.0 version This is part of Collection framework introduced in Java 1.2 version This is part of Collection framework introduced in Java 1.2 version
Using Enumeration interface, we can enumerate only legacy classes like Hashtable or Vector or Properties Iterator interface is applicable for every collection classes like ArrayList, HashSet or Hashtable ListIterator interface is applicable only for List objects like ArrayList or LinkedList or Vector
We can enumerate legacy collection items only in FORWARD direction Here, too we can iterate through collection items only in FORWARD direction But with ListIterator, we can iterate through list items either in FORWARD or BACKWARD directions
That is, it is unidirectional or single directional cursor That is, it is unidirectional or single directional cursor That is, it is bi-directional cursor
Using Enumeration interface, we can enumerate to read or get element/object from legacy collection Using Iterator interface, we can read as well as remove collection items, while iterating Addition or replacement of new objects is possible alongside read and remove operation in ListIterator interface
To get an Enumeration object, we can use elements() method of any legacy collection class

For example,

Vector v = new Vector();

Enumeration e = v.elements();

To get an Iterator object, we can use iterator() method of any collection class

For example,

Iterator itr = col.iterator();

Where col = any collection class

To get a ListIterator object, we can use listIterator() method of any List classes

For example,

ListIterator ltr = list.listIterator();

Where list = any List objects

Enumeration interface has 2 important methods to enumerate through legacy collection objects

boolean hasMoreElements();

Object nextElement();

Iterator interface has 3 important methods to iterate through any collection objects

boolean hasNext();

Object next();

void remove();

ListIterator interface has 9 important methods to iterate through any List objects

Read here, for details of ListIterator methods

 

Best practice:

Enumeration interface:

Use this cursor only with legacy collection, to work with thread-safe environment

 

Iterator interface:

This is very popular among 3 cursors, as it is applicable for any collection class

 

ListIterator interface:

Again, this is applicable only for List objects. Use this cursor, to benefit from iterating through List items in both directions (FORWARD & BACKWARD)

 

References:

https://docs.oracle.com/javase/tutorial/collections/intro/
https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html
https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html

 

Happy Coding !!
Happy Learning !!