The Enumeration interface in Java is one of the legacy interfaces used to iterate over elements of collections such as Stack, Vector and HashTable. It was introduced in JDK 1.0 and is part of the java.util package
- It is a legacy cursor used before Iterator and ListIterator.
- It supports only forward direction traversal (one-way).
- Enumeration isn’t fail-fast, but Vector and Hashtable are, so modifying them during Enumeration can still cause ConcurrentModificationException.
- Mostly used with Vector, Stack, and Hashtable classes.
Declaration
public interface Enumeration<E>
Where E is the type of elements stored in a collection.
Creating Enumeration Object
Creating an Enumeration Object with the Vector class
Vector v = new Vector();
Enumeration e = v.elements();
import java.util.*;
public class GFG{
public static void main(String[] args){
Vector<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Getting Enumeration object
Enumeration<String> e = fruits.elements();
System.out.println("Elements of Vector:");
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
}
}
Output
Elements of Vector: Apple Banana Cherry
Explanation
- The elements() method of the Vector class returns an Enumeration of the vector’s elements.
- The hasMoreElements() method checks if more elements exist in the collection.
- The nextElement() method retrieves the next element and advances the cursor.
- The iteration continues until all elements are processed.
Methods of Enumeration Interface
- asIterator(): This method returns an Iterator which traverses all the remaining elements covered by this enumeration.
- hasMoreElements(): It returns a boolean indicating whether more elements remain; returns false when all elements are traversed.
- nextElement(): It returns the next element in the enumeration sequence
Example: Using Enumeration with Hashtable
import java.util.*;
public class GFG{
public static void main(String[] args){
Hashtable<Integer, String> map = new Hashtable<>();
map.put(1, "Java");
map.put(2, "Python");
map.put(3, "C++");
Enumeration<Integer> keys = map.keys();
System.out.println("Hashtable Keys:");
while (keys.hasMoreElements()) {
int key = keys.nextElement();
System.out.println(key + " -> " + map.get(key));
}
}
}
Output
Hashtable Keys: 3 -> C++ 2 -> Python 1 -> Java
Working of Enumeration
Let’s understand the working process of the Enumeration cursor step-by-step. Suppose we have a Vector of three elements — Apple, Banana, and Cherry
Step 1: Before Iteration
- The cursor lies before the first element.
- hasMoreElements() -> returns true (next element exists).
- Calling nextElement() returns "Apple" and moves the cursor forward.