Search
Thursday 21 September 2023
  • :
  • :

Java Concepts – How Enumeration And Iterator Differs In Java?

Enumeration and iterator are two interfaces in Java that are designed to traverse the collection objects In Java. However, both interfaces designed with single core objective yet they are different from each other somehow.

In the case of Enumeration, you could only traverse over the elements of collection object while iterator has the skill to remove the element of the collection object. It was the major difference between Enumeration and iterator interfaces.

Java dev

There are other differences too that we will discuss in this blog post.

  • Enumeration interface introduced in JDK 1.0 and Iterator introduced in JDK 1.2.
  • Remove () Method is the major difference between the two interfaces. In the case of Enumeration, you could only traverse over the elements of collection object while iterator has the skill to remove the element of the collection object. So iterator has removed () 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. The iterator doesn’t have any legacy code.
  • Enumeration throws exceptions while iterator does not throw any exception when the collection object is modified. The iterator does not allow collection modification, so it is much highly safe and secure than Enumeration.

Conclusion

For most of the times, Iterator is preferred over Enumeration by expert Java developers and programmers. The functional capability of the iterator is almost double as compared to enumeration.

Enumeration Iterator
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




Leave a Reply