Explain Enumeration vs Iterator interface
Both Enumeration and Iterator are used to traverse collections in Java, but they have significant differences in functionality and usage.
Table of Contents
Enumeration vs Iterator interface
1. Introduction
- Enumeration is an interface in the java.util package.
- It is a legacy interface that was used in the earlier versions of Java (before Java 2 Collections Framework).
2. Methods
- hasMoreElements(): Returns true if there are more elements in the enumeration.
- nextElement(): Returns the next element in the enumeration.
3. Limitations
- Does not support element removal during iteration.
- Limited functionality compared to Iterator.
Iterator Introduction
- Iterator is an interface in the java.util package introduced with the Java 2 Collections Framework.
- It is part of the modern collection framework and is the preferred way to traverse collections.
2. Methods:
- hasNext(): Returns true if the iteration has more elements.
- next(): Returns the next element in the iteration.
- remove(): Removes from the underlying collection the last element returned by this iterator (optional operation).
3. Advantages
- Â Â Supports safe removal of elements during iteration.
- Â Â More functionality and flexibility than Enumeration.
java
Here’s an example demonstrating the use of both Enumeration and Iterator with a Vector:
import java.util.Vector;
import java.util.Enumeration;
import java.util.Iterator;
public class EnumerationVsIterator {
public static void main(String[] args) {
// Create a Vector and add some elements
Vector<String> vector = new Vector<>();
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
vector.add("Date");
// Using Enumeration to traverse the Vector
System.out.println("Using Enumeration:");
Enumeration<String> enumeration = vector.elements();
while (enumeration.hasMoreElements()) {
String element = enumeration.nextElement();
System.out.println(element);
}
// Using Iterator to traverse the Vector
System.out.println("\nUsing Iterator:");
Iterator<String> iterator = vector.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
// Removing elements using Iterator
iterator = vector.iterator(); // Reinitialize the iterator
while (iterator.hasNext()) {
String element = iterator.next();
if (element.startsWith("B")) {
iterator.remove(); // Remove elements that start with 'B'
}
}
// Display the Vector after removal
System.out.println("\nVector after removal using Iterator:");
for (String element : vector) {
System.out.println(element);
}
}
}
Explanation Enumeration vs Iterator interface
- Using Enumeration:
- An Enumeration is obtained from the Vector using the elements() method.
- The while loop uses hasMoreElements() to check if there are more elements and nextElement() to retrieve each element.
- Output: Using Enumeration:
Apple
Banana
Cherry
Date
Using Iterator
- An Iterator is obtained from the Vector using the iterator() method.
- The while loop uses hasNext() to check if there are more elements and next() to retrieve each element.
- Output: Using Iterator:
Apple
Banana
Cherry
Date
Removing Elements Using Iterator:
- The iterator is reinitialized to traverse the Vector again.
- During iteration, elements starting with ‘B’ are removed using the remove() method.
- Output: Vector after removal using Iterator:
Apple
Cherry
Date