Java集合-EnumMap工作原理及實(shí)現(xiàn)

【轉(zhuǎn)載】 原文鏈接 - (部分修改與補(bǔ)充)


  • 更多相關(guān)文章見筆者博客

1.概述

A specialized Map implementation for use with enum type keys. All of the keys in an enum map must come from a single enum type that is specified, explicitly or implicitly, when the map is created. Enum maps are represented internally as arrays. This representation is extremely compact and efficient.

EnumMap是是一種鍵為枚舉類型的特殊的Map實(shí)現(xiàn)。所有的Key也必須是一種枚舉類型,EnumMap是使用數(shù)組來(lái)實(shí)現(xiàn)的。(兩個(gè)數(shù)組,一個(gè)數(shù)組keyUniverse存儲(chǔ)key,另一個(gè)數(shù)組vals存儲(chǔ)val,兩個(gè)數(shù)組通過(guò)下標(biāo)對(duì)應(yīng)起來(lái)

EnumMap<Course, String> map = new EnumMap<Course, String>(Course.class);
map.put(Course.ONE, "語(yǔ)文");
map.put(Course.ONE, "政治");
map.put(Course.TWO, "數(shù)學(xué)");
map.put(Course.THREE, "英語(yǔ)");
for(Entry<Course, String> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
}

輸出結(jié)果(按照枚舉類中的順序遍歷數(shù)組,按數(shù)組下標(biāo)依次輸出(跳過(guò)空))為:

ONE: 政治
TWO: 數(shù)學(xué)
THREE: 英語(yǔ)

其具體實(shí)現(xiàn)的結(jié)構(gòu)如下圖所示:

image

2. put和get方法

put方法通過(guò)key的ordinal將值存儲(chǔ)到對(duì)應(yīng)的地方,get方法則根據(jù)key的ordinal獲取對(duì)應(yīng)的值。

public V put(K key, V value) {
    // 類型檢查
    typeCheck(key);
    // 獲取key的序號(hào)
    int index = key.ordinal();
    Object oldValue = vals[index];
    // 賦值
    vals[index] = maskNull(value);
    // 若之前的值為空,則size++
    if (oldValue == null)
        size++;
    return unmaskNull(oldValue);
}

public V get(Object key) {
    return (isValidKey(key) ?
            unmaskNull(vals[((Enum<?>)key).ordinal()]) : null);
}

3. 遍歷

EnumMapIterator的迭代這樣實(shí)現(xiàn)的:

public boolean hasNext() {
    while (index < vals.length && vals[index] == null)
        index++;
    return index != vals.length;
}

public Map.Entry<K,V> next() {
    if (!hasNext())
        throw new NoSuchElementException();
    lastReturnedEntry = new Entry(index++);
    return lastReturnedEntry;
}

通過(guò)hasNext跳過(guò)空的數(shù)組,也就是說(shuō),保證了遍歷順序與Enumkey的先后順序一致。

參考資料

What is EnumMap in Java

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Java集合類可用于存儲(chǔ)數(shù)量不等的對(duì)象,并可以實(shí)現(xiàn)常用的數(shù)據(jù)結(jié)構(gòu)如棧,隊(duì)列等,Java集合還可以用于保存具有映射關(guān)...
    小徐andorid閱讀 2,099評(píng)論 0 13
  • Chapter 6 Enums and Annotations 枚舉和注解 JAVA supports two s...
    LaMole閱讀 1,009評(píng)論 0 2
  • 傳送門:Java(Android)數(shù)據(jù)結(jié)構(gòu)匯總 -- 總綱 簡(jiǎn)介 這篇主要來(lái)整理下基于Map接口實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)類。...
    sens_bingo閱讀 2,112評(píng)論 0 54
  • 對(duì)象的創(chuàng)建與銷毀 Item 1: 使用static工廠方法,而不是構(gòu)造函數(shù)創(chuàng)建對(duì)象:僅僅是創(chuàng)建對(duì)象的方法,并非Fa...
    孫小磊閱讀 2,186評(píng)論 0 3
  • 這是汪汪。 我們認(rèn)識(shí)有快四年了。 2014年初來(lái)上海,在虹口區(qū)租了個(gè)老房子。汪汪住一樓,我住三樓。說(shuō)起來(lái)我們是鄰居...
    無(wú)敵無(wú)聊麻子姐姐閱讀 214評(píng)論 0 1

友情鏈接更多精彩內(nèi)容