Difference between Iterator and Enumeration in Java

JavaObject Oriented ProgrammingProgramming

Iterator and Enumeration both are the cursors to traverse and access an element from the collection. They both belong to the collection framework. Enumeration was added in JDK1.0 and Iterator in the JDK.1.2 version in the collection framework. 

Enumeration can’t make structural changes in the collection because it has read-only access to the element in the collection. It has the following methods :

  • *hasMoreElements()
  • *nextElement()

On the other hand, an iterator can read and remove the element in the collection. It has the following methods −

  • *hasNext()
  • *next()
  • *remove()
Sr. No.KeyIteratorEnumeration
1
Basic
In Iterator,  we can read and remove element while traversing element in the collections. 
Using Enumeration, we can only read element during traversing element in the collections.
2. 
Access 
It can be used with any class of the collection framework.
It can be used only with legacy class of the collection framework such as a Vector and HashTable.
3. 
Fail-Fast and Fail -Safe 
Any changes in the collection, such as removing element from the collection during a thread is iterating collection then it throw concurrent modification exception. 
Enumeration  is Fail safe in nature. It doesn’t throw concurrent modification exception 
4. 
Limitation 
Only forward direction iterating is possible
Remove operations can not be performed using Enumeration.
5.
Methods 
It has following methods −
*hasNext()
*next()
*remove()
 It has following methods −
*hasMoreElements()
*nextElement()

Example of Enumeration

class EnumerationExample {
   public static void main(String args[]) {
      List list = new ArrayList(Arrays.asList( new String[] {"Apple", "Cat", "Dog", "Rat"}));
      Vector v = new Vector(list);
      delete(v, "Dog");
   }
   private static void delete(Vector v, String name) {
      Enumeration e = v.elements();
      while (e.hasMoreElements()) {
         String s = (String) e.nextElement();
         if (s.equals(name)) {
            v.remove(name);
         }
      }
      // Display the names
      System.out.println("The names are:");
      e = v.elements();
      while (e.hasMoreElements()) {
         // Prints elements
         System.out.println(e.nextElement());
      }
   }
}

Example of Iterator

class IteratorExample {
   public static void main(String args[]) {
      List list = new ArrayList(Arrays.asList( new String[] {"Apple", "Cat", "Dog", "Rat"}));
      Vector v = new Vector(list);
      delete(v, "Dog");
   }
   private static void delete(Vector v, String name) {
      Iterator i = v.iterator();
      while (i.hasNext()) {
         String s = (String) i.next();
         if (s.equals(name)) {
            i.remove();
         }
      }
      // Display the names
      System.out.println("The names are:");
      i = v.iterator();
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}
raja
Published on 18-Nov-2019 10:56:33
Advertisements