About NewTechnoBuzz
Advertise
Contact Us

Monday, July 14, 2014

Difference between Enumeration and Iterator

This tutorial explains about what are the differences between Iterators and Enumeration and similarity between them. Although, this questions I didn't face from past couple of years, but still it is an important question in respect of core Java interview questions. Sometimes, interviewer asks this difference at beginner level (2-3 years of experience). The functionalities of both Iterator & Enumeration interfaces are similar and are used to traverse or navigate through entire collection in java. The more powerful newer interface Iterator takes place of the old interface Enumeration in the Java Collections Framework.

Iterator

Iterator in Java is nothing just a traversing object, which is used to traverse Collection objects like List, Set and Map etc. It allows us to traverse the collection object and access the data element of collection without bothering the user about specific implementation of that collection.

It has three methods:
  • hasNext()
  • next()
  • remove()

import java.util.Iterator;
import java.util.ArrayList;
 
public class NewTechIteratorExample {
 
  public static void main(String[] args) {
   
    ArrayList arrList = new ArrayList();

    arrList.add("1");
    arrList.add("2");
    arrList.add("3");
    arrList.add("4");
    arrList.add("5");
   
   
    System.out.println("ArrayList before removal of element: ");
    for(int index = 0; index < arrList.size(); index++) {
      System.out.println(arrList.get(index));
    }
     
    Iterator itr = arrList.iterator();
    while(itr.hasNext()){
      String str = (String)itr.next();
      if(str.equals("2"))
      {
        itr.remove();
        break;
      }
    }
   
    System.out.println("ArrayList after removal of element : ");
    for(int index = 0; index < arrList.size(); index++) {
      System.out.println(arrList.get(index));
    }
  }
}

This would produce the following result:

ArrayList before removal of element :
1
2
3
4
5
ArrayList after removal of element :
1
3
4
5

Enumeration

It is also used to traverse (enumerate) the elements of a collection. But, those collections are part of legacy framework (i.e classes like HashTable, Vector etc.). This interface is almost deprecated and superceded by Iterator.

It has following methods:
  • hasMoreElements()
  • nextElement()

import java.util.Vector;
import java.util.Enumeration;

public class NewTechEnumerationTest {

   public static void main(String args[]) {
      Enumeration days;
      Vector dayNames = new Vector();
      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");
      days = dayNames.elements();
      while (days.hasMoreElements()){
         System.out.println(days.nextElement()); 
      }
   }
}

This would produce the following result:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Iterator provides more functionality as compare to Enumeration but it also has some drawbacks. Let us walk through the similarities and differences of both of them.

  • Iterator has a remove() method while Enumeration doesn't. Enumeration acts as Read-only interface, because it has methods only to traverse the collection objects, whereas Iterator can manipulate the collection objects like removing the objects from collection e.g. Arraylist.
  • Iterators are more secure and safe as compared to Enumeration because it does not allow a thread to modify the collection object while some other thread is iterating over it. If a thread tries to modify the collection while some other thread is traversing then iterator fails quickly by throwing ConcurrentModificationException.

I hope this article helped you to understand. Please feel free to post any comment or question if you have any problem and find difficulty in understanding the article.

0 comments