Enumeration vs Iterator
There are many data structures that act as collections in Java such as Vectors, Hash tables and classes that implements Java Collections Framework (i.e. HashMap, HashSet, ArrayList, TreeSet, TreeMap, LinkedList, LinkedHashMap and LinkedHashSet). There are numerous ways to iterate through the individual elements of the objects in Java. Java provides two interfaces to make this task easier. Enumeration and Iterator are two of the interfaces found in java.util package that provide functionality to enumerate through sequences or objects with a set of items. Enumerator was introduced in JDK 1.0 and Iterator which was introduced in JDK 1.2 virtually duplicates the functionality of the Enumerator (within the Collections Framework).
What is Enumeration?
Enumeration is a public interface in Java, introduced in JDK 1.0, which provides the ability to enumerate through sequences of elements. It is found under java.util package. When the Enumeration interface is implemented by an object, that object can generate a sequence of elements. Enumeration interface has two methods. The method hasMoreElements() will test if this enumeration contains more elements and the nextElement() returns the next element in the sequence (if there is at least one more to go). In other words, by calling nextElement() successively, the programmer can access the individual elements in the series. For example, to print all elements in Vector v1 using Enumerator, the following code snippet can be used.
Enumeration e = v1.elements();
Enumerator can also be used to define the stream of input to the SequenceInputStream objects.
What is Iterator?
Iterator is a public interface in Java.util package, which allows iterating through elements of the collections objects that implement the Collections framework (such as ArrayList, LinkedList, etc.). This was introduced in JDK 1.2 and replaced the Enumerator within the Java Collections Framework. Iterator has three methods. The method hasNext() tests whether there are remaining elements in the collection and the next() method returns the next element in the series. The remove() method can be used to remove the current element from the underlying collection. For example, to print all elements in Vector v1 using Iterator, the following code snippet can be used.
Iterator i = v1.elements();
What is the difference between Enumeration and Iterator?
Although, Enumeration and Iterator are two of the interfaces found in java.util package, which allow iterating/enumerating through elements of a series, they have their differences. Actually, Iterator, which was introduced after Enumeration, replaces the Enumeration within the Java Collections framework. Unlike Enumeration, Iterator is fail-safe. This means that concurrent modifications (to the underlying collection) are not allowed when Iterator is used. This is very useful in multi-threaded environments where there is always a risk of concurrent modifications. In the event of a concurrent modification, the Iterator object will throw a ConcurrentModificationException. Iterator has shorter method names compared to Enumerator. Furthermore, iterator has the additional functionality of deleting elements during the iteration (which is not possible using Enumerator). So, if there is a need to remove elements from the collection, Iterator is the only option that can be considered.