Difference Between Iterator and Enumeration in Java!

Hello Learners, today we are going to learn about the difference between Iterator and Enumeration in Java. Iterator and Enumeration both interfaces are used to traverse over a Collection by selecting one element at a time. but there are some differences between them, let’s see the points you get a basic idea of how they are different from the other methods.

Iterator Interface:

  1. It was introduced in JDK 1.2
  2. Iterators were introduced later it is used with most of the Collection classes like ArrayList, LinkedList, HashSet etc. but iterators can not be used with legacy classes like Vectors, Stack, HashTables etc.
  3. If a collection is modified while traversing by another method instead of its own remove method then it throws ConcurrentModificationException. at runtime.
  4. It is considered as thread-safe because it doesn’t allow other threads to modify the collection while traversing.
  5. Iterators have three major methods described below:
    • 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.

How To Use an Iterator Object in Java

import java.util.Iterator; // --> import Iterators in your code
//creating object reference of iterator 
Iterator<Integer> i = c.iterator();
 
//here c is any collection object like arraylist, hashset etc. 
//traversing through the elements of collection c 

while(i.hasNext()) // --> checks whether the collection has more items. 
{ 
    //print the next item 
    System.out.println(i.next()); 
}
  • NOTE: This was just a pseudo code for comparison between iterators and enumerations.

Enumeration Interface:

  1. It was introduced in JDK 1.0
  2. Enumerations were introduced earlier so it is used with the legacy classes like Vectors, Stack, HashTables, Properties. but it can be used with the other collection classes also.
  3. Enumerations are Fail-Safe in nature. If a collection is modified during traversing then it doesn’t throw any exception.
  4. It isn’t thread-safe because it allows the other threads to modify the collection during traversing.
  5. Enumerations have two major methods described below:
    • 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.

Point to be remembered: It does not have a remove method.

How To Use an Enumeration Object in Java

 
import java.util.Enumeration; // --> import Enumerations in your code
//creating object reference of enumeration 
Enumeration<Integer> e = c.elements(); 
//here c is any collection object like stack, vectors etc. 
//traversing through the elements of collection c

while(e.hasMoreElements()) // --> checks whether the collection has more items. 
{ 
    //print the next item 
    System.out.println(e.nextElement()); 
}
  • This was just a pseudo code for comparison but You can learn more about Enumeration by clicking on the link.

So, that’s all for now about the difference between Iterator and Enumeration in Java. Till then Keep Learning, Keep Practicing, Keep Reading.

Leave a Reply