Java記錄 -71- HashMap源碼剖析
HashMap底層維護著一個數組,我們向HashMap中所放置的對象實際上是儲存在該數組當中。數組中每個元素都維護著一個鏈表,每個鏈表中的所有元素的hash值都一樣。HashMap的四個構造方法:底層維護的數組 Entry[] table,數組預設大小為16,並為2的整數次冪;底層數組存放的Entry對象,是HashMap的一個內部類,包含了key,value和next變數;
/** * The table, resized as necessary. Length MUST Always be a power of two. */ transient Entry[] table; static class Entry<K,V> implements Map.Entry<K,V> { final K key; V value; Entry<K,V> next; final int hash; ...... } 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); // Find a power of 2 >= initialCapacity int capacity = 1; while (capacity < initialCapacity) capacity <<= 1; this.loadFactor = loadFactor; threshold = (int)(capacity * loadFactor); table = new Entry[capacity]; init(); } public HashMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } public HashMap() { this.loadFactor = DEFAULT_LOAD_FACTOR; threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR); table = new Entry[DEFAULT_INITIAL_CAPACITY]; init(); } public HashMap(Map<? extends K, ? extends V> m) { this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1, DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR); putAllForCreate(m); }
下面來看做關心的添加方法put:當添加的key為null時,會取出table的第一個元素,並在該元素所領導的鏈表中尋找key==null的元素,有則替換,沒有則添加;key不為null時,需要計算key的hash值,調用一個函數式,根據經驗計算出一個整數值來;然後再根據計算出的hash值和數組table的長度計算出新添加元素在數組中的位置;從底層數組中取出上面計算的位置的元素值,即Entry對象,從該對象領導的鏈表中尋找key相等的對象,有則替換;如果上一步沒有找到相等的對象,則添加新對象;將對象添加到底層數組table中,位置為上面計算得到的位置值,並將該對象的next指向該位置以前存放的元素;
public V put(K key, V value) { if (key == null) return putForNullKey(value); int hash = hash(key.hashCode()); 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++; addEntry(hash, key, value, i); return null; } private V putForNullKey(V value) { for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) { V oldValue = e.value; e.value = value; e.recordAccess(this); return oldValue; } } modCount++; addEntry(0, null, value, 0); return null; } static int hash(int h) { // This function ensures that hashCodes that differ only by // constant multiples at each bit position have a bounded // number of collisions (approximately 8 at default load factor). h ^= (h >>> 20) ^ (h >>> 12); return h ^ (h >>> 7) ^ (h >>> 4); } static int indexFor(int h, int length) { return h & (length-1); } void addEntry(int hash, K key, V value, int bucketIndex) { Entry<K,V> e = table[bucketIndex]; table[bucketIndex] = new Entry<K,V>(hash, key, value, e); if (size++ >= threshold) resize(2 * table.length); }
所有集合存放元素就是為了讀取使用,下面來看看取元素:get方法key為null時需要特殊處理,即從底層數組table的第一個元素中尋找key==null的值返回;如果key不為null,則需要計算key的hash值,根據該值在table中取得一個元素,然後再在該元素所在的鏈表中尋找對應key的值返回;
public V get(Object key) { if (key == null) return getForNullKey(); int hash = hash(key.hashCode()); for (Entry<K,V> e = table[indexFor(hash, table.length)]; e != null; e = e.next) { Object k; if (e.hash == hash && ((k = e.key) == key || key.equals(k))) return e.value; } return null; } private V getForNullKey() { for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; } return null; }
常用的方法還有:containsKey,remove,keySet,entrySet,values這些方法都需要先在底層數組中找,然後遍曆對應的鏈表。 根據設定的閥值,但達到閥值時,HashMap的底層數組將進行擴容。 新加的對象為什麼添加到鏈的頭部,而不是尾部?為了提高效能,作業系統中有這麼一個原則,認為最新添加到鏈中的元素下次訪問的幾率要大於以前的;因此添加到鏈表頭部,如果下次訪問該元素時就可以第一個訪問到,設想如果放到尾部則需要變數整個鏈表來找到該元素。 HashMap添加一個對象的思路過程:當向HashMap中put一對索引值時,它會根據key的hashCode值計算出一個位置,該位置就是此對象準備往數組中存放的位置。如果該位置沒有對象存在,就將此對象直接放進數組當中;如果該位置已經有對象存在了,則順著此存在的對象的鏈開始尋找(Entry類有一個Entry類型的next成員變數,指向了該對象的下一個對象),如果此鏈上有對象的話,再去使用equals方法進行比較;如果對此鏈上的某個對象的key比較為true,則證明已經存在該對象,此時將用新對象替換舊對象;如果對此鏈上的所有對象的equals方法比較都為false,則將該對象放到數組當中,然後將數組中該位置以前存在的那個對象連結到此對象的後面。