In this post, we will explain the difference between the Enumeration and Iterator interfaces in Java, and provide some examples on how to use them.

Java provides two interfaces to access and modify the elements of a collection: Enumeration and Iterator. Though they serve a similar purpose, there are crucial distinctions between the two that we must understand.

1. Overview of the Enumeration interface

The Enumeration interface is an interface that has been present in Java since version 1.0. It is used to iterate over a collection of objects, such as Vector, HashTable and Stack. It offers two primary methods: hasMoreElements() and nextElement(), using which we can check if there are more elements and move to those elements.

 
Note that the Enumeration interface does not have any methods to remove or modify elements from the collection. It is a read-only interface that only allows traversal of the collection. Let’s see an example of how to use the Enumeration interface:

Download  Run Code

2. Overview of the Iterator interface

The Iterator interface is an interface that has been present in Java since version 1.2. It is used to iterate over any collection of objects, such as List, Set, Map, etc. It offers three primary methods: hasNext(), next(), and remove():

 
Using the hasNext() and the next() methods, we can check if there are more elements and move to those elements. Using the remove() method, we can remove the last element returned by the next() method from the collection. The remove() method is a default method, so it is optional to implement it. However, if it is not implemented, it will throw an UnsupportedOperationException. Let’s see an example of how to use the Iterator interface:

Download  Run Code

3. Difference between Enumeration and Iterator in Java

The functionality of Enumeration and Iterator are similar. They both allow us to access elements in a collection sequentially without knowing its underlying structure. However, they also have some differences and trade-offs that we must understand:

  1. The Enumeration interface can only be used with legacy classes, such as Vector or Hashtable, while the Iterator interface can be used with any collection class, such as List, Set, Map, etc. The Iterator officially took the place of Enumeration in the Java Collections Framework.
  2. The Enumeration interface has only two methods: hasMoreElements() and nextElement(), while the Iterator interface has three methods: hasNext(), next(), and remove().
  3. The Enumeration interface does not have any method to remove or modify elements from the collection, while the Iterator interface has a method to remove elements from the collection.
  4. The Enumeration interface is a read-only interface that only allows traversal of the collection, while the Iterator interface is a read-write interface that allows both traversal and modification of the collection.
  5. Enumeration is fail-safe in nature while an Iterator is fail-fast in nature.
  6. Enumeration in Java moves only in the forward direction. There is another special type of Iterator, called ListIterator, which facilitates bi-directional access.
  7. The Iterator has shorter method names – hasMore() and next(), as opposed to hasMoreElements() and nextElement() of Enumeration interface.

As we have seen, Enumeration and Iterator are both useful interfaces to iterate over collections in Java. However, if we need a more efficient and advanced interface to iterate over any collection, we should use the enhanced for-loop or the forEach() method introduced in Java 8.

That’s all about the differences between Enumeration and Iterator interface in Java.