English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

The Difference Between Iterator and Enumeration in Java

Iterator and Enumeration are cursors used to traverse and access elements in a collection. They both belong to the collection framework. In the JDK of the collection framework1.0 and JDK.1.2The version of Iterator added enumeration. 

Enumeration cannot make structural changes to the collection because it has read-only access to the elements in the collection. It has the following methods:

  • * hasMoreElements()

  • * nextElement()

On the other hand, the iterator can read and delete elements in the collection. It has the following methods-

  • * hasNext()

  • *nex()

  • *remove()

indexkeyiteratorenumeration
1a
Basic

In Iterator, we can read and delete elements while traversing the elements of a collection.

We can only read elements when traversing the elements of a collection using enumeration.
2. 
Access 

It can be used with any class of the collection framework.

It can only be used with the old classes of the collection framework, such as Vector and HashTable.
3. 
Failure-Failure and failure-Safety 

Any changes to the collection, such as removing elements from the collection while iterating over it in a thread, will cause ConcurrentModificationException.

The enumeration is essentially fault-tolerant. It will not cause ConcurrentModificationException 
4. 
Limitations 
It can only be used for forward iterationYou cannot perform delete operations using enumeration.
5,
Method 

It has the following methods -

*hasNext()
*next()
*remove()
 It has the 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 name
      System.out.println("The names are:");
      i = v.iterator();
      while (i.hasNext()) {
         System.out.println(i.next());
      }
   }
}