Enumeration vs Iterator

Enumeration vs Iterator

After practicing Enumeration and Iterator with examples, let us summarize the differences. Both Enumeration and Iterator are used for iterating elements of a data structure. Iterator, being latest, comes with more features tabulated below.

Enumeration Iterator
Introduced with JDK 1.0 Introduced with JDK 1.2
Cannot remove any elements Can remove elements from the original DS itself
Methods are hasMoreElements() and nextElement() Methods are hasNext() and next()
Methods are difficult to remember Methods are easy to remember

Both nextElement() and next() return an object of Object class. Both iterate in forward direction only (remember, ListIterator can move backwards also).

Example on Enumeration of Enumeration vs Iterator


ss
Output screen on Enumeration vs Iterator

Another example with explanation is available at Enumeration Interface.

Example on Iterator of Enumeration vs Iterator


ss
Output screen on Enumeration vs Iterator

Another example with explanation is available at Iterator Interface.

3 thoughts on “Enumeration vs Iterator

Leave a Reply

Your email address will not be published. Required fields are marked *