1. HashMap的實現原理之 HashMap數據結構:算法
HashMap是對數據結構中哈希表(Hash Table)的實現, Hash表又叫散列表。Hash表是根據關鍵碼Key來訪問其對應的值Value的數據結構。數組
它經過一個映射函數把關鍵碼Key映射到Hash表中一個位置來訪問該位置的值Value,從而加快查找的速度。這個映射函數叫作Hash函數,存放記錄的數組叫作Hash表。數據結構
在Java中,HashMap的內部實現結合了鏈表和數組的優點,連接節點的數據結構是Entry<k,v>,每一個Entry對象的內部又含有指向下一個Entry類型對象的引用,如如下代碼所示:less
1 static class Entry<K,V> implements Map.Entry<K,V> { 2 final K key; 3 V value; 4 Entry<K,V> next; //Entry類型內部有一個本身類型的引用,指向下一個Entry 5 final int hash; 6 ... 7 }
哈希表有多種不一樣的實現方法,我接下來解釋的是最經常使用的一種方法--- 拉鍊法,咱們能夠理解爲"鏈表的數組" ,如圖:函數
2. HashMap的實現原理之 HashMap的存取實現:性能
既然是線性數組,爲何能隨機存取?這裏HashMap用了一個小算法,大體是這樣實現:優化
1 // 存儲時: 2 int hash = key.hashCode(); // 這個hashCode方法這裏不詳述,只要理解每一個key的hash是一個固定的int值 3 int index = hash % Entry[].length; 4 Entry[index] = value; 5 6 // 取值時: 7 int hash = key.hashCode(); 8 int index = hash % Entry[].length; 9 return Entry[index];
(1)putthis
這裏HashMap裏面用到鏈式數據結構的一個概念。上面咱們提到過Entry類裏面有一個next屬性,做用是指向下一個Entry。打個比方, 第一個鍵值對A進來,經過計算其key的hash獲得的index=0,記作:Entry[0] = A。一會後又進來一個鍵值對B,經過計算其index也等於0,如今怎麼辦?HashMap會這樣作:B.next = A,Entry[0] = B,若是又進來C,index也等於0,那麼C.next = B,Entry[0] = C;這樣咱們發現index=0的地方其實存取了A,B,C三個鍵值對,他們經過next這個屬性連接在一塊兒。因此疑問不用擔憂。也就是說數組中存儲的是最後插入的元素,HashMap同一index下使用頭插法(每次插入數據,從鏈頭部插入)。spa
到這裏爲止,HashMap的大體實現,咱們應該已經清楚了。code
public V put(K key, V value) { if (key == null) return putForNullKey(value); //null老是放在數組的第一個鏈表中 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; //若是key在鏈表中已存在,則替換爲新value 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; } 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); //參數e, 是Entry.next //若是size超過threshold,則擴充table大小。再散列 if (size++ >= threshold) resize(2 * table.length); }
固然HashMap裏面也包含一些優化方面的實現,這裏也說一下。好比:Entry[]的長度必定後,隨着map裏面數據的愈來愈長,這樣同一個index的鏈就會很長,會不會影響性能?
回答:會影響性能,HashMap裏面設置一個因子,隨着map的size愈來愈大,Entry[](對應index的鏈表,每一個元素都是Entry)會以必定的規則加長長度。
(2)get
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; }
(3)null key 的存取
null key老是存放在Entry[]數組的第一個元素。
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; } private V getForNullKey() { for (Entry<K,V> e = table[0]; e != null; e = e.next) { if (e.key == null) return e.value; } return null; }
(4)肯定數組的index:hashcode % table.length取模
HashMap存取時,都須要計算當前key應該對應Entry[]數組哪一個元素,即計算數組下標;算法以下:
/** * Returns index for hash code h. */ static int indexFor(int h, int length) { return h & (length-1); }
public HashMap(int initialCapacity, float 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(); }
注意table初始大小並非構造函數中的initialCapacity!!
而是 >= initialCapacity的2的n次冪!!!!
3. HashMap的實現原理之 解決hash衝突的辦法:
Java中hashmap的解決辦法就是採用的鏈地址法。
4. HashMap的實現原理之 哈希表rehash過程(擴容機制):
當HashMap中的元素愈來愈多的時候,hash衝突的概率也就愈來愈高,由於數組的長度是固定的。因此爲了提升查詢的效率,就要對HashMap的數組進行擴容,數組擴容這個操做也會出如今ArrayList中,這是一個經常使用的操做,而在HashMap數組擴容以後,最消耗性能的點就出現了:原數組中的數據必須從新計算其在新數組中的位置,並放進去,這就是resize。
當哈希表的容量超過默認容量時,必須調整table的大小。當容量已經達到最大可能值時,那麼該方法就將容量調整到Integer.MAX_VALUE返回,這時,須要建立一張新表,將原表的映射到新表中。
HashMap 類中包含3個和擴容相關的常量:
DEFAULT_INITIAL_CAPACITY 是初始容量,默認是 2^4 = 16;
MAXIMUM_CAPACITY是最大容量,默認是 2^30;
DEFAULT_LOAD_FACTOR是增加因子,當佔用率超過這個值時,就會觸發擴容操做。
DEFAULT_INITIAL_CAPACITY是table數組的容量,DEFAULT_LOAD_FACTOR則是爲了最大程度避免哈希衝突,提升HashMap效率而設置的一個影響因子,將DEFAULT_LOAD_FACTOR乘以DEFAULT_INITIAL_CAPACITY就獲得了一個閾值threshold,當HashMap的容量達到threshold時就須要進行擴容,這個時候就要進行ReHash操做了,能夠看到下面addEntry函數的實現,當size達到threshold時會調用resize()函數進行擴容。
HashMap的默認擴容機制,是存儲的key超過容量的75%時,容量翻番。其實,這些和有序無序不要緊。
好比:當前大小是16,當佔用超過16*0.75=12時,就把容量擴充到16*2=32。
resize()方法的源碼以下:
1 /** 2 * Rehashes the contents of this map into a new array with a 3 * larger capacity. This method is called automatically when the 4 * number of keys in this map reaches its threshold. 5 * 6 * If current capacity is MAXIMUM_CAPACITY, this method does not 7 * resize the map, but sets threshold to Integer.MAX_VALUE. 8 * This has the effect of preventing future calls. 9 * 10 * @param newCapacity the new capacity, MUST be a power of two; 11 * must be greater than current capacity unless current 12 * capacity is MAXIMUM_CAPACITY (in which case value 13 * is irrelevant). 14 */ 15 void resize(int newCapacity) { 16 Entry[] oldTable = table; 17 int oldCapacity = oldTable.length; 18 if (oldCapacity == MAXIMUM_CAPACITY) { 19 threshold = Integer.MAX_VALUE; 20 return; 21 } 22 Entry[] newTable = new Entry[newCapacity]; 23 transfer(newTable); 24 table = newTable; 25 threshold = (int)(newCapacity * loadFactor); 26 } 27 28 29 30 /** 31 * Transfers all entries from current table to newTable. 32 */ 33 void transfer(Entry[] newTable) { 34 Entry[] src = table; 35 int newCapacity = newTable.length; 36 for (int j = 0; j < src.length; j++) { 37 Entry<K,V> e = src[j]; 38 if (e != null) { 39 src[j] = null; 40 do { 41 Entry<K,V> next = e.next; 42 //從新計算index 43 int i = indexFor(e.hash, newCapacity); 44 e.next = newTable[i]; 45 newTable[i] = e; 46 e = next; 47 } while (e != null); 48 } 49 } 50 }
在擴容的過程當中須要進行ReHash操做,而這是很是耗時的,在實際中應該儘可能避免。