Difference between Iterator and Enumeration

Keywords: Java JDK

This article introduces the difference and efficiency between Iterator and Enumeration

Iterator is an interface. Its source code is as follows:

package java.util;

import java.util.function.Consumer;

public interface Iterator<E> {
    //Returns the reference of the element that the iterator just crossed. The return value is Object,Need to cast to the type you need
    boolean hasNext();
    //Determine whether there are accessible elements in the container,The return value is E
    E next();
   //Delete the element just passed by the iterator
    default void remove() {
        throw new UnsupportedOperationException("remove");
    }

    default void forEachRemaining(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        while (hasNext())
            action.accept(next());
    }
}

Enumeration is also an interface. Its source code is as follows:

package java.util;

public interface Enumeration<E> {
    
    boolean hasMoreElements();

    E nextElement();
}

From the source code, it can be seen that the Iterator can not only read the data of the collection, but also delete the data; while Enumeration can only read the data of the collection, not modify the data.

Iterator supports the fail fast mechanism, while enumeration does not. Enumeration is the interface added by JDK 1.0. The functions used include Vector, Hashtable and other classes, which are added in JDK 1.0. Iterator is an interface added by JDK 1.2. Iterator is implemented based on enumeration. At the same time, iterator supports fail fast mechanism, so iterator will traverse the collection slower than enumeration.

Use a Hashtable collection, and then traverse it through Iterator and Enumeration to compare their efficiency. The code is as follows:

package com.xyfer;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

public class Test {
    public static void main(String[] args) {
        int n;
        Random r = new Random();
        Hashtable t = new Hashtable();
        for (int i = 0; i < 10000; i++) {
            n = r.nextInt(1000);
            t.put(i, n);
        }
        iterateHashtable(t);
        enumeration(t);
    }
    //Use Iterator ergodic Hashtable
    private static void iterateHashtable(Hashtable t) {
        long start = System.currentTimeMillis();
        Iterator i = t.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry entry = (Entry) i.next();
            //System.out.println("key:" + entry.getKey() + "value:" + entry.getValue());
        }
        long end = System.currentTimeMillis();
        useTime(start,end);
    }
    //Use Enumeration ergodic Hashtable
    private static void enumeration(Hashtable t) {
        long start = System.currentTimeMillis();
        Enumeration enu = t.elements();
        while (enu.hasMoreElements()) {
            enu.nextElement();
            //Enumeration em = (Enumeration) enu.nextElement();
            //System.out.println(enu.nextElement());
        }
        long end = System.currentTimeMillis();
        useTime(start,end);
    }
    //Computation traversal Hashtable Consumption time
    private static void useTime(long start,long end) {
        System.out.println("Time consuming:"+(end-start)+"ms");
    }

}

Console print results:

From the control printing result, Iterator does traverse the collection slower than Enumeration.

Posted by plowter on Thu, 05 Dec 2019 22:57:40 -0800