本源碼研究是基於JDK1.8的,JDK版本不一樣,會有略微的差異。
1 什麼是HashMap
HashMap是一種以key-value形式存儲數據,數據存儲結構以數組與鏈表爲結構存儲數據的,其中以hash算法獲得數據的存儲位置,高效管理存取的一種數據結構。算法
2 HashMap的基本原理圖數組
3 下面咱們以斷點調試的方式進行源碼分析
public static void main ( String[] arg ) {數據結構
HashMap<String,Object> hashMap = new HashMap<>(); hashMap.put( "1","50" ); hashMap.put( "2","lisi" ); hashMap.put( "3","50" ); hashMap.put( "4","lisi" ); hashMap.put( "5","50" ); hashMap.put( "6","lisi" ); hashMap.put( "7","50" ); hashMap.put( "8","50" ); hashMap.put( "9","lisi" ); hashMap.put( "10","50" ); hashMap.put( "11","50" ); hashMap.put( "12","50" ); hashMap.put( "price","50" );
}
經過斷點調試,首先會進入
public V put(K key, V value) {函數
return putVal(hash(key), key, value, false, true);
}
這個方法,這個方法hash(key)函數爲注入對象,緊接着就會進入這個方法之中
static final int hash(Object key) {源碼分析
int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); (1)
}
在方法(1)中key.hashCode()直接調用了string類的hashCode()的值,其中的原理,請看String有關的源碼解讀,會返回h=3373707,而後拿3373707的二進制與它的二進制數右移16位獲得的結果進行異或運算 獲得h=3373752。緊接着會進入this
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,spa
boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; (2) if ((tab = table) == null || (n = tab.length) == 0) (3) n = (tab = resize()).length; (4) if ((p = tab[i = (n - 1) & hash]) == null) (5) tab[i] = newNode(hash, key, value, null); (6) else { Node<K,V> e; K k; if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) (7) e = p; else if (p instanceof TreeNode) (8) e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value); (9) else { for (int binCount = 0; ; ++binCount) { (10) if ((e = p.next) == null) { (11) p.next = newNode(hash, key, value, null); (12) if (binCount >= TREEIFY_THRESHOLD - 1) (13) treeifyBin(tab, hash); (14) break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) (15) break; p = e; } } if (e != null) { (16) V oldValue = e.value; (17) if (!onlyIfAbsent || oldValue == null) (18) e.value = value; (19) afterNodeAccess(e); (20) return oldValue; } } ++modCount; (21) if (++size > threshold) (22) resize(); (23) afterNodeInsertion(evict); (24) return null; (25) }
的核心方法之中,在(3)中首先會判斷 table是否爲空,由於是首次進入,必定爲空,就會經過(4)的方法建立一個大小爲16,擴容閥值爲12的table數組,而且將數據賦值給tab,經過(5)便可獲得下標值爲8(h&15)便可獲得。而後在經過方法(6)就能夠獲得一個Node,key是「name」,value是「zhangshan」,而後判斷一下是否超過了閥值,是否進行擴容。調試
當hashMap.put( "name","zhangshan" );運行這個方法的時候,putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict)咱們同理,分析下過程,在(5)中,因爲已經有值了,因此會直接進入到(7)中,經過(7)的判斷,會講key的值賦值給e,經過(17)-(19)將新的值付給e,將原來的值付給oldValue中間變量,將原來的值返回,固然,經過 String info = ( String ) hashMap.put( "name","wangwu" );能夠得來原來的值。
當運行hashMap.put( "price","50" ); 的時候,正好到了HashMap擴容的界限,那麼,就會在(23)進入resize()的方法,
final Node<K,V>[] resize() {code
Node<K,V>[] oldTab = table; (30) int oldCap = (oldTab == null) ? 0 : oldTab.length; (31) int oldThr = threshold; (32) int newCap, newThr = 0; if (oldCap > 0) { if (oldCap >= MAXIMUM_CAPACITY) { (33) threshold = Integer.MAX_VALUE; return oldTab; } else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) (34) newThr = oldThr << 1; // double threshold } else if (oldThr > 0) // initial capacity was placed in threshold newCap = oldThr; else { // zero initial threshold signifies using defaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } if (newThr == 0) { float ft = (float)newCap * loadFactor; newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE); } threshold = newThr; @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; (35) table = newTab; if (oldTab != null) { for (int j = 0; j < oldCap; ++j) { Node<K,V> e; if ((e = oldTab[j]) != null) { oldTab[j] = null; if (e.next == null) newTab[e.hash & (newCap - 1)] = e; (36) else if (e instanceof TreeNode) ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); else { // preserve order Node<K,V> loHead = null, loTail = null; Node<K,V> hiHead = null, hiTail = null; Node<K,V> next; do { next = e.next; if ((e.hash & oldCap) == 0) { if (loTail == null) loHead = e; else loTail.next = e; loTail = e; } else { if (hiTail == null) hiHead = e; else hiTail.next = e; hiTail = e; } } while ((e = next) != null); if (loTail != null) { loTail.next = null; newTab[j] = loHead; } if (hiTail != null) { hiTail.next = null; newTab[j + oldCap] = hiHead; } } } } } return newTab; }
經過(30)-(32),建立一個oldTab 將原有的table保存,而且保存了閥值和大小 oldCap和oldThr,而後在(34)將oldCap和oldThr擴容2倍,而且分給新的中間變量newThr、newCap,
而後在(35)新建立一個擴容後的大小的table,在(36)的地方會將原來的table裏面的值從新取出hash而後進行新的位置計算,而後把原來的值直接賦值給新的table數組的具體下標的位置,將當前entry的next鏈指向新的索引位置,newTable[i]有可能爲空,有可能也是個entry鏈,若是是entry鏈,直接在鏈表頭部插入。對象