7 Difference Between Iterator And Enumeration

SHARE

Iterator

An Iterator is an object that enables a programmer to traverse a container, particularly lists. It is used with most of the collection classes like ArrayList, LinkedList, HashSet, TreeMap, TreeSet etc but iterators cannot be used with legacy classes like Vectors, Stack, HashTables etc. Iterator allows to remove elements from the underlying collection during the iteration using its remove() method.

Iterators have three major methods:

  • hasNext(): a boolean type method that returns true or false if the collection has more items remaining after the current item.
  • next(): an object type method that selects the next item of the current item.
  • remove(): this method allows you to remove any item from the collection even while traversing it.

What You Need To Know About Iterator

  • Iterator was introduced later and is used with most of the collection classes like ArrayList, LinkedList, HashSet, TreeMap, TreeSet etc but iterators cannot be used with legacy classes like Vectors, Stack, HashTables etc.
  • Iterator is Fail-Fast in nature. It throws ConcurrentModificationException at runtime if a Collection is modified while iterating other than its own remove () method.
  • Iterator can do modifications e.g using remove () method, it removes the element from the Collection during traversal.
  • Iterator was introduced in the second version of java i.e JDK 1.2.
  • Limitations of Iterator are resolved by ListIterator.
  • The object of Iterator can read and remove the elements from the collection.

Enumeration

Enumeration is an interface that allows obtaining one element at a time in a collection of objects.  Enumeration was introduced earlier and it is used with legacy classes like Vectors, Stack, HashTables, Properties but it can be used with the other collection classes. We cannot remove elements from a collection when using enumerator.

Enumerations have two major methods:

  • hasMoreElements(): a boolean type method that returns true or false if the collection has more items remaining after the current item. It is similar to hasNext() method.
  • nextElement(): an object type method that selects the next item of the current item. It is similar to next() method.

What You Need To Know About Enumeration

  • Enumeration was introduced earlier and it is used with legacy classes like Vectors, Stack, HashTables, Properties but it can be used with the other collection classes.
  • Enumerations are Fail-Safe in nature. It does not throw ConcurrentModificationException if a Collection is modified during the traversal.
  • Enumeration is considered as read only interface, one cannot do any modifications to Collection while traversing the elements of the Collection.
  • Enumeration was introduced in first version of Java i.e JDK 1.0.
  • Limitations of Enumeration are resolved by Iterator.
  • Enumeration object has only read-only access to the elements in the collection.

Also Read: Difference HashTable And HashMap

Difference Between Iterator And Enumeration In Tabular Form

BASIS OF COMPARISON ITERATOR ENUMERATION
Description Iterator was introduced later and is used with most of the collection classes. Enumeration was introduced earlier and it is used with legacy classes.
Nature Iterator is Fail-Fast in nature. Enumerations are Fail-Safe in nature.
Modifications Iterator can do modifications e.g using remove () method, it removes the element from the Collection during traversal. Enumeration is considered as read only interface, one cannot do any modifications to Collection while traversing the elements of the Collection.
Introduction Iterator was introduced in the second version of java i.e JDK 1.2. Enumeration was introduced in first version of Java i.e JDK 1.0.  
Limitations Limitations of Iterator are resolved by ListIterator. Limitations of Enumeration are resolved by Iterator.
Object reading The object of Iterator can read and remove the elements from the collection. Enumeration object has only read-only access to the elements in the collection.
Methods Iterators have three major methods: hasNext()next()remove() Enumerations have two major methods: hasMoreElements()nextElement()

1 COMMENT

Comments are closed.