1.

Iterator vs Enumeration in Java

Answer»

Both Iterator and Enumeration are interfaces BELONGING to the java.util package. They are used for traversal of collection objects.

Although Iterator and Enumeration have the same functionality of traversal, there are quite a few DIFFERENCES between them. Some of the differences are given below:

Basis
Iterator
Enumeration
Introduction
Iterator interface was introduced in JDK 1.2
Enumeration interface existed since JDK 1.0
Functions
Iterator can be used for traversal as well as to remove an element from a given Collection object.
Enumeration can only perform traversal through the collection object.
Nature
Iterator is fail-fast is nature. It throws a ConcurrentModificationException if any modification other than the remove() method  is done to the collection while iteration.
Enumeration is fail-safe in nature. It does not throw any exceptions during modification of the Collection Object during an iteration.
Legacy
Classes like ArrayList, HASHSET and HashMap in the collection framework are traversed by Iterator
Traversal of legacy classes like Vector and Stack is done through Enumeration
Safety
Iterator is safer and more robust than Enumeration
Enumeration is more vulnerable due to its fail-safe nature.
Methods
hasNext()  
next()
remove()
hasMoreElements()
NEXTELEMENT()
-


Discussion

No Comment Found