java -- Iterator and Enumeration comparison

java -- Iterator and Enumeration comparison

 

 

where is the difference

 

      In Java collections, we usually use "Iterator (iterator)" or "Enumeration (enumeration class)" to traverse the collection. Take a look at the following source code:

 

Enumeration is an interface, and its source code is as follows:

package java.util;

public interface Enumeration<E> {

    boolean hasMoreElements();

    E nextElement();
}

 

 

Iterator is also an interface, and its source code is as follows:

package java.util;

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

    E next();

    void remove();
}

 

 

From the above source code, we can see the difference

 

different functional interfaces

 

  • Enumeration has only 2 functional interfaces. Through Enumeration, we can only read the data of the collection, but not modify the data.
  • Iterator has only 3 functional interfaces. Iterator can not only read the data of the collection, but also delete the data.

 

 

Iterator supports fail-fast mechanism, while Enumeration does not.

 

  • Enumeration is an interface added in JDK 1.0. The functions that use it include classes such as Vector and Hashtable. These classes were added in JDK 1.0. The purpose of Enumeration is to provide them with a traversal interface. Enumeration itself does not support synchronization, but when Vector and Hashtable implement Enumeration, synchronization is added.
  • Iterator is an interface added in JDK 1.2. It also provides a traversal interface for collections such as HashMap and ArrayList. Iterator supports fail-fast mechanism: when multiple threads operate on the contents of the same collection, fail-fast events may be generated.

 

 

 

Iterator and Enumeration instances

 

      Next, we write a Hashtable, and then traverse it through Iterator and Enumeration respectively, and compare their efficiency. code show as below:

 

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

/*
 * Test to traverse 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++) {
            // Get a random number between [0,100)
            val = r.nextInt(100);
            table.put(String.valueOf(i), val);
        }

        // Traverse Hashtable through Iterator
        iterateHashtable(table) ;

        // Traverse Hashtable through Enumeration
        enumHashtable(table);
    }
    
    /*
     * Traverse 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);
    }
    
    /*
     * Traverse 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");
    }
}

 

The results are as follows:

time: 9ms

time: 5ms

 

From this, we can see that. Enumeration traverses faster than Iterator.

 

 

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 naturally performed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326969644&siteId=291194637