java集合18--Iterator和Enumeration比較

轉載地址:http://blog.csdn.net/wangxiaotongfan/article/details/51346289

概要

這一章,我們對Iterator和Enumeration進行比較學習。內容包括: 

第1部分 Iterator和Enumeration區別 
第2部分 Iterator和Enumeration實例

第1部分 Iterator和Enumeration區別

在Java集合中,我們通常都通過 “Iterator(迭代器)” 或 “Enumeration(枚舉類)” 去遍歷集合。今天,我們就一起學習一下它們之間到底有什麼區別。

我們先看看 Enumeration.java 和 Iterator.java的源碼,再說它們的區別。

Enumeration是一個接口,它的源碼如下:

package java.util;

public interface Enumeration<E> {

    boolean hasMoreElements();

    E nextElement();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

Iterator也是一個接口,它的源碼如下:

package java.util;

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

    E next();

    void remove();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

看完代碼了,我們再來說說它們之間的區別。

(01) 函數接口不同 
Enumeration只有2個函數接口。通過Enumeration,我們只能讀取集合的數據,而不能對數據進行修改。 
Iterator只有3個函數接口。Iterator除了能讀取集合的數據之外,也能數據進行刪除操作。

(02) Iterator支持fail-fast機制,而Enumeration不支持。 
Enumeration 是JDK 1.0添加的接口。使用到它的函數包括Vector、Hashtable等類,這些類都是JDK 1.0中加入的,Enumeration存在的目的就是爲它們提供遍歷接口。Enumeration本身並沒有支持同步,而在Vector、Hashtable實現Enumeration時,添加了同步。 
而Iterator 是JDK 1.2才添加的接口,它也是爲了HashMap、ArrayList等集合提供遍歷接口。Iterator是支持fail-fast機制的:當多個線程對同一個集合的內容進行操作時,就可能會產生fail-fast事件。

第2部分 Iterator和Enumeration實例

下面,我們編寫一個Hashtable,然後分別通過 Iterator 和 Enumeration 去遍歷它,比較它們的效率。代碼如下:

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

/*
 * 測試分別通過 Iterator 和 Enumeration 去遍歷Hashtable
 * @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++) {
            // 隨機獲取一個[0,100)之間的數字
            val = r.nextInt(100);
            table.put(String.valueOf(i), val);
        }

        // 通過Iterator遍歷Hashtable
        iterateHashtable(table) ;

        // 通過Enumeration遍歷Hashtable
        enumHashtable(table);
    }

    /*
     * 通過Iterator遍歷Hashtable
     */
    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);
    }

    /*
     * 通過Enumeration遍歷Hashtable
     */
    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");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

運行結果如下:

time: 9ms
time: 5ms
  • 1
  • 2

從中,我們可以看出。Enumeration 比 Iterator 的遍歷速度更快。爲什麼呢? 
這是因爲,Hashtable中Iterator是通過Enumeration去實現的,而且Iterator添加了對fail-fast機制的支持;所以,執行的操作自然要多一些。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章