hashMap源碼分析

一、hashMap基於數組和鏈表的,那麼如何對hashMap進行擴容呢?,這裏須要進行二次判斷是否是超過閾值了,若是超過閾值則須要對hashMap進行擴容。 數組

二、hashMap的容量爲hashMap中桶的數量,即數組的大小。 安全

三、hashMap的size爲hashMap中鍵值對的個數,即kv鍵值對的個數。 多線程

四、hashMap的實現,看put和get源碼,最主要的也是這個兩個函數的實現,本源碼是基於jdk1.7的源碼實現。函數

  • 首先來看看put的源代碼:

public V put(K key, V value) {
    //若是爲空,則默認給值
    if (table == EMPTY_TABLE) {
        inflateTable(threshold);
    }
   //容許key爲空
    if (key == null)
        return putForNullKey(value);
    int hash = hash(key);
//拿到當前的容器的下標
    int i = indexFor(hash, table.length);
    for (Entry<K,V> e = table[i]; e != null; e = e.next) {
        Object k;
//若是在原來的容器中有值則替換以前的值
        if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue;
        }
    }

    modCount++;
//不然就添加到新的table數組當中
    addEntry(hash, key, value, i);
    return null;

再來看看addEntry的代碼是如何解析的? this

void addEntry(int hash, K key, V value, int bucketIndex) {
    if ((size >= threshold) && (null != table[bucketIndex])) {
        resize(2 * table.length);   //擴容爲原來的2倍
        hash = (null != key) ? hash(key) : 0;//獲得hash值
        bucketIndex = indexFor(hash, table.length); //獲得數組的位置
    }

    createEntry(hash, key, value, bucketIndex); //建立一個entry
}

resize的源碼也很是精彩: spa

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); //從新給一個閾值
}

其中有一個transfer函數寫的很是巧妙:線程

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;
        }
    }
}

每次遍歷舊的table的鏈表,在從新計算每一個鏈表值在容器中的位置,最後將值給到數組的第一個位置,即鏈表的頭(和插入時的用法是同樣的,都是插入鏈表的頭,這段很精彩)。 code

再來看看createEntry的源碼:blog

void createEntry(int hash, K key, V value, int bucketIndex) {
    Entry<K,V> e = table[bucketIndex];
    table[bucketIndex] = new Entry<>(hash, key, value, e);
    size++;
}

能夠看到每次都是將最新添加的元素放在鏈表的第一個元素位置,也就是table中的第一個元素。 ci

  • 接着分析get源碼是如何得到數據的:
    public V get(Object key) {
        if (key == null)
            return getForNullKey();
        Entry<K,V> entry = getEntry(key);
    
        return null == entry ? null : entry.getValue();
    }

主要是看getEntry的代碼

final Entry<K,V> getEntry(Object key) {
    if (size == 0) {
        return null;
    }

    int hash = (key == null) ? 0 : hash(key);  //若是key爲空的話hash爲0
    for (Entry<K,V> e = table[indexFor(hash, table.length)];  //拿到當前table爲0的數組的鏈表
         e != null;
         e = e.next) {
        Object k;
        if (e.hash == hash &&
            ((k = e.key) == key || (key != null && key.equals(k))))
            return e;
    }
    return null;
}

從get和put能夠分析得出HashMap是線程不安全的,沒有加鎖處理機制,若是多個線程訪問同一個Map會形成數據不正確的問題,若是在多線程環境下,採用ConcurrentHashMap能夠解決線程不安全的問題。

相關文章
相關標籤/搜索