Difference between Enumeration and Iterator interface in Java
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.
|
1 2 3 4 |
public interface Enumeration<E> { boolean hasMoreElements(); E nextElement(); } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Arrays; import java.util.Enumeration; import java.util.Vector; class Main { public static void main(String[] args) { // Create a Vector object Vector<String> names = new Vector<>(Arrays.asList("Aaron", "Bella", "Casey")); // Get an Enumeration object for the vector Enumeration<String> enumeration = names.elements(); // Iterate over the vector using the enumeration while (enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } // Aaron // Bella // Casey } } |
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():
|
1 2 3 4 5 6 7 |
public interface Iterator<E> { boolean hasNext(); E next(); default void remove() { throw new UnsupportedOperationException("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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; class Main { public static void main(String[] args) { // Create a List object List<String> names = new ArrayList<>(Arrays.asList("Aaron", "Bella", "Casey")); // Get an Iterator object for the list Iterator<String> iterator = names.iterator(); // Iterate over the list using the iterator while (iterator.hasNext()) { String name = iterator.next(); if ("Bella".equals(name)) { // Remove Bella from the list iterator.remove(); } } // Print the updated list System.out.println(names); // [Aaron, Casey] } } |
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:
- The
Enumerationinterface can only be used with legacy classes, such asVectororHashtable, while theIteratorinterface can be used with any collection class, such asList,Set,Map, etc. TheIteratorofficially took the place ofEnumerationin the Java Collections Framework. - The
Enumerationinterface has only two methods:hasMoreElements()andnextElement(), while theIteratorinterface has three methods:hasNext(),next(), andremove(). - The
Enumerationinterface does not have any method to remove or modify elements from the collection, while theIteratorinterface has a method to remove elements from the collection. - The
Enumerationinterface is a read-only interface that only allows traversal of the collection, while theIteratorinterface is a read-write interface that allows both traversal and modification of the collection. Enumerationis fail-safe in nature while anIteratoris fail-fast in nature.Enumerationin Java moves only in the forward direction. There is another special type of Iterator, called ListIterator, which facilitates bi-directional access.- The
Iteratorhas shorter method names –hasMore()andnext(), as opposed tohasMoreElements()andnextElement()ofEnumerationinterface.
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)