Java – 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

So let’s us discuss in tabular format;

1. Enumeration v/s Iterator v/s ListIterator:

  • All 3 cursors are used to iterate over collection items
  • but there are certain differences between each one of them
EnumerationIteratorListIterator
This is part of Legacy collection introduced in Java 1.0 versionThis is part of Collection framework introduced in Java 1.2 versionThis 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 PropertiesIterator interface is applicable for every collection classes like ArrayList, HashSet or HashtableListIterator interface is applicable only for List objects like ArrayList or LinkedList or Vector
We can enumerate legacy collection items only in FORWARD directionHere, too we can iterate through collection items only in FORWARD directionBut with ListIterator, we can iterate through list items either in FORWARD or BACKWARD directions
That is, it is unidirectional or single directional cursorThat is, it is unidirectional or single directional cursorThat is, it is bi-directional cursor
Using Enumeration interface, we can enumerate to read or get element/object from legacy collectionUsing Iterator interface, we can read as well as remove collection items, while iteratingAddition 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

2. Best practice:

2.1 Enumeration interface:

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

2.2 Iterator interface:

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

2.3 ListIterator interface:

  • Again, this is applicable only for List objects.
  • Use this cursor, to benefit from iterating through List items in both directions
  • i.e.; both FORWARD & BACKWARD directions
  • List Iterator

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Set interface with example
Java - ListIterator interface