HashMap

java version "1.7.0_67"java

HashMap的草圖:redis

Entry數組:數組

static final Entry<?,?>[] EMPTY_TABLE = {};
transient Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
transient int size; // map中鍵值對數量

構造函數只是設置了 loadFactor 和 threshold 的值,因此table仍是空的。安全

裝填因子loadFactor默認爲0.75,loadFactor = 鍵值對數量 / 數組大小數據結構

public HashMap(int initialCapacity, float loadFactor) {
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal initial capacity: " +
                                           initialCapacity);
    if (initialCapacity > MAXIMUM_CAPACITY)
        initialCapacity = MAXIMUM_CAPACITY;
    if (loadFactor <= 0 || Float.isNaN(loadFactor))
        throw new IllegalArgumentException("Illegal load factor: " + loadFactor);

    this.loadFactor = loadFactor; //默認0.75
    threshold = initialCapacity; //默認16
    init();
}

void init() { }

簡單分析put操做:函數

public V put(K key, V value) {
    //第一次put時,table是空的,須要擴容
    if (table == EMPTY_TABLE) {
        inflateTable(threshold);
    }
    //null也能夠做爲鍵
    if (key == null)
        return putForNullKey(value);
    //根據key的hashCode計算出hash值
    int hash = hash(key);
    //根據hash值,定位到table中的位置
    int i = indexFor(hash, table.length);
    //若是已存在相等key,更新value,並返回舊value
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
        // 兩個key相等的判斷條件:
        //1. key的hash值相等,key的地址相等
        //2. key的hash值相等,key用equals比較相等
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }

    modCount++;
    //不存在相等的key,則使用頭插法加入鏈表中
    addEntry(hash, key, value, i);
    return null;
}

其實HashMap的內部數據結構仍是比較清晰的,日常用得也挺多,聽到最多的說法就是HashMap不是線程安全的,緣由簡單提一句,是在resize的過程當中可能會產生環。this

分析下HashMap resize的過程:HashMap的數據遷移是一次性的,相對而言,redis的作法比較有趣,把數據遷移分攤到get和set操做上。當map中鍵值對數量超過threshold時,不必定會發生resize。spa

void addEntry(int hash, K key, V value, int bucketIndex) {
    // 若是map中鍵值對的數量達到了threshold,且當前槽掛着entry
    if ((size >= threshold) && (null != table[bucketIndex])) {
        //2倍擴展
        resize(2 * table.length);
        hash = (null != key) ? hash(key) : 0;
        //擴展以後,table指向了新的數組,length也變了,從新計算index
        bucketIndex = indexFor(hash, table.length);
    }

    createEntry(hash, key, value, bucketIndex);
}

void resize(int newCapacity) {
    Entry[] oldTable = table;
    int oldCapacity = oldTable.length;
    if (oldCapacity == MAXIMUM_CAPACITY) {
        threshold = Integer.MAX_VALUE;
        return;
    }

    Entry[] newTable = new Entry[newCapacity];
    transfer(newTable, initHashSeedAsNeeded(newCapacity));
    table = newTable;
    threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}

void transfer(Entry[] newTable, boolean rehash) {
    int newCapacity = newTable.length;
    for (Entry<K,V> e : table) {
        while(null != e) {
            Entry<K,V> next = e.next;
            if (rehash) {
                e.hash = null == e.key ? 0 : hash(e.key);
            }
            int i = indexFor(e.hash, newCapacity);
            //這裏也是使用頭插法,因此原來鏈表的順序會翻轉
            e.next = newTable[i];
            newTable[i] = e;
            e = next;
        }
    }
}
相關文章
相關標籤/搜索