Enumeration and iterator are two interfaces in Java that are designed to traverse the collection objects In Java. However, both interfaces were designed with single core objective yet they are different from each other somehow.
In case of Enumeration, you could only traverse over the elements of collection object while iterator has the capability to remove the element of collection object. This was the major difference between Enumeration and iterator interfaces.
There is other differences too that we will discuss in this blog post.
- Enumeration interface was introduced in JDK 1.0 and Iterator was introduced in JDK 1.2.
- Remove () Method is the major difference between two interfaces. In case of Enumeration, you could only traverse over the elements of collection object while iterator has the capability to remove the element of collection object. So iterator has remove () method while Enumeration has not any remove () method.
- Enumeration is generally used to traverse legacy classes while Iterator traverses the classes within collection framework only. Iterator doesn’t have any legacy code.
- Enumeration throws exceptions while iterator does not throw any exception when collection object is modified. As iterator does not allow collection modification so they are highly safe and secure in nature as compared to Enumeration.
For most of the times, Iterator is preferred over Enumeration by expert Java developers and programmers. The functional capability of iterator is almost double as compared to enumeration.
|Enumeration interface was introduced in JDK 1.0||Iterator was introduced in JDK 1.2|
|For Enumeration, you could only traverse over the elements of collection objects.||Iterator has the capability to remove the element of collection object.|
|Traverses legacy classes||Don’t have any legacy code|
|Less Reliable||Safe and secure|
|It throws exceptions||It doesn’t throw exceptions|
This guest blog has been written by Shipra Jain working as a technical writer with Aegis Softwares for more than 5 years. She is writing on Java technologies and frameworks from years. She has appreciable understanding about the concepts and other computer languages too.