• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Iterator and Enumeration Interface in Java

Iterator vs EnumerationThe three cursors that are used to access the elements from any collection one by one are Enumeration, Iterator and ListIterator. Though, Iterator and Enumerator are meant for performing the same work. Still, they are distinct from each other in a sense that Enumeration has read-only access to the elements in the collection. On the other side, Iterator can read as well as remove the elements from the collection.

The important difference between the Iterator and Enumeration is that the Enumeration can’t be applied on the collection classes it is only applicable on the legacy classes. On the other hand, the Iterator is applicable to the collection classes hence, it is referred as a universal cursor. Let us learn some more difference between Iterator and Enumeration with the help of the comparison chart shown below.

Content: Iterator Vs Enumeration

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonIterator Enumeration
BasicIterator is a universal cursor as it is applicable for all the collection classes.Enumeration is not a universal cursor as it applies only to legacy classes.
AccessUsing Iterator you can read and remove the elements in the collection.Using Enumeration you can only read the elements in the collection.
Methodspublic boolean hasnext();
public objects next();
public void remove();
public boolean hasMoreElements();
public object nextElement();
LimitationIterator is a unidirectional forward access cursor.
Iterator can not replace any element in the collection.
Iterator can not add any new element in the collection.
Enumeration is unidirectional forward access cursor.
Enumeration support only legacy classes.
Enumeration has only read-only access to the elements in a collection.
OvercomeTo overcome the limitations of Iterator you must opt for ListIterator.To overcome the limitations of Enumeration you must opt for Iterator.

Definition of Iterator Interface

Iterator is an interface in the collection framework. As Iterator is applicable to all the collection classes, it is referred as a universal cursor.  It is the cursor used to access the elements in the collection one by one. Using Iterator, you can retrieve the elements from the collection and if you want you can also remove the elements from the collection. The object of Iterator can be created as given below.

Iterator itr = Collc.iterator();

The variable itr is an object of Iterator. Collc is any collection object which is to be cycled or iterated by using the object (itr) of the Iterator. The iterator() is the method used to create an iterator object. There are three methods in the Iterator as shown below.

public boolean hasnext();
public Object next();
public void remove();

The first method hasnext() checks whether the collection has any elements in it or not. If the collection has the elements, it will return true else will return false. The second method next() is used to retrieve the next element in the collection. The third method remove() is used to delete the elements from the collection.

Iterator can travel in a collection only in the forward direction it can’t move back while travelling. Iterator can remove the element from the collection but does not have the capability to replace any existing element with a new element neither it can add any new element in the collection. To overcome these limitations you can go for ListIterator interface.

Definition of Enumeration Interface

Enumeration is the interface applicable to the legacy classes, and it can not be applied to any collection class. Hence, it is not a universal cursor. Enumeration retrieves the element (object) from the collection one by one. Enumeration object has read-only access to the elements of the collection. Enumeration object can not change any element from the collection. Let us see how to create enumeration object, have a look.

Enumeration er = Vect.elements();

The variable er is an object of Enumeration. The Vect is the object of the vector class that has to be traversed by the object (er) of Enumeration. The method element() is used to create an object of Enumeration. There are only two methods in the Enumeration interface as shown below.

public boolean hasMoreElements();
public nextElement();

The first method hasMoreElements() is used to check the status of the collection whether it has the elements in it or it is empty. If the collection has the elements the hasMoreElements() method return true else return false. The second method nextElement() is used to retrieve the elements from the collection one by one.

When traversing is complete the nextElement() method throws NoSuchElementException. The Enumeration object travels only in the forward direction. It can not add or remove or replace any element in the collection. To overcome these limitations of the Enumeration, you must opt for Iterator.

Key Differences Between Iterator and Enumeration in Java

  1. The main difference between Iterator and Enumeration is that Iterator is a universal cursor, can be used for iterating any collection object. On the other hand, the Enumeration is used for traversing object of legacy class only.
  2. Enumeration object has only read-only access to the elements in the collection. However, the object of Iterator can read and remove the elements from the collection.
  3. There are two methods of iterator one to check the status of collection and one to retrieve the elements from the collection. In addition to Enumeration methods Iterator has one more method to remove the elements from the collection.
  4. Limitations of Enumeration are it is forward unidirectional cursor, it has read-only access, and it can be applied to the collection classes. On the other way, Iterator can not replace or add any new element in the collection and like Enumeration its also forward unidirectional cursor.
  5. Limitation of Enumeration are resolved by Iterator and limitations of Iterator are resolved by ListIterator.

Conclusion

Nowadays, Iterator and ListItertaor cursor are used as they are the universal cursor and are much efficient than Enumeration.

Related Differences:

  1. Difference Between Iterator and ListIterator in Java
  2. Difference Between ArrayList and Vector in Java
  3. Difference Between HashMap and Hashtable in Java
  4. Difference Between Interface and Abstract Class in Java & C#
  5. Difference Between ArrayList and LinkedList in Java

Comments

  1. Vignesh says

    June 13, 2021 at 11:33 am

    Clean and clear explanation

    Reply

Leave a Reply Cancel reply

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

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy