Comparison between Iterator and Enumeration

Difference between Iterator and Enumeration

In Java collections, we usually traverse the collection through "iterator" or "enumeration".

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

package java.util;

public interface Enumeration<E> {

    boolean hasMoreElements();

    E nextElement();
}

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

package java.util;

public interface Iterator<E> {
    boolean hasNext();

    E next();

    void remove();
}

Enumeration has only 2 function interfaces. With Enumeration, we can only read the data of the collection, not modify the data.

Iterator has only three function interfaces. In addition to reading the data of the collection, iterator can also delete the data.

Iterator supports the fail fast mechanism, while Enumeration does not. Iterator is an interface added only by JDK 1.2. It also provides traversal interface for collections such as HashMap and ArrayList. Iterator supports the fail fast mechanism: when multiple threads operate on the content of the same collection, fail fast events may occur.

Iterator and Enumeration instances

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

/*
 * The tests traverse the Hashtable through Iterator and Enumeration respectively
 * @author skywang
 */
public class IteratorEnumeration {

    public static void main(String[] args) {
        int val;
        Random r = new Random();
        Hashtable table = new Hashtable();
        for (int i=0; i<100000; i++) {
            // Randomly obtain a number between [0100]
            val = r.nextInt(100);
            table.put(String.valueOf(i), val);
        }

        // Traversing Hashtable through Iterator
        iterateHashtable(table) ;

        // Traversing Hashtable through Enumeration
        enumHashtable(table);
    }
    
    /*
     * Traversing Hashtable through Iterator
     */
    private static void iterateHashtable(Hashtable table) {
        long startTime = System.currentTimeMillis();

        Iterator iter = table.entrySet().iterator();
        while(iter.hasNext()) {
            //System.out.println("iter:"+iter.next());
            iter.next();
        }

        long endTime = System.currentTimeMillis();
        countTime(startTime, endTime);
    }
    
    /*
     * Traversing Hashtable through Enumeration
     */
    private static void enumHashtable(Hashtable table) {
        long startTime = System.currentTimeMillis();

        Enumeration enu = table.elements();
        while(enu.hasMoreElements()) {
            //System.out.println("enu:"+enu.nextElement());
            enu.nextElement();
        }

        long endTime = System.currentTimeMillis();
        countTime(startTime, endTime);
    }

    private static void countTime(long start, long end) {
        System.out.println("time: "+(end-start)+"ms");
    }
}

Output result

time: 9ms
time: 5ms

From this, we can see. Enumeration is faster than Iterator traversal. Why? This is because the Iterator in Hashtable is implemented through Enumeration, and the Iterator adds support for the fail fast mechanism; therefore, more operations are performed naturally.

Keywords: Java JDK

Added by raahatazim on Mon, 30 Dec 2019 19:56:40 +0200