一、putVal()源碼
/** * Associates the specified value with the specified key in this map. * If the map previously contained a mapping for the key, the old * value is replaced. * * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with key, or * null if there was no mapping for key. * (A null return can also indicate that the map * previously associated null with key.) */ public V put(K key, V value) { return putVal(hash(key), key, value, false, true); } /** * Implements Map.put and related methods * * @param hash hash for key * @param key the key * @param value the value to put * @param onlyIfAbsent if true, don’t change existing value * @param evict if false, the table is in creation mode. * @return previous value, or null if none */ final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { # Node[] tab是key被hash后存儲(chǔ)的哈希桶數(shù)組,是一個(gè)單向鏈表數(shù)組 Node[] tab; Node p; int n, i; # 如果哈希桶數(shù)組是null或者length==0,進(jìn)行擴(kuò)容操作,初始化哈希桶數(shù)組的容量為16 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; # 如果在取模運(yùn)算后的索引位置的數(shù)組元素是null,直接插入新的鏈表元素 if ((p = tab[i = (n – 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); # 如果tab[i]的位置是一個(gè)長(zhǎng)度于等于1的鏈表,就在最后一個(gè)節(jié)點(diǎn)next追加新的節(jié)點(diǎn) else { Node e; K k; # 如果已經(jīng)有相同hash和相同的k if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; # 如果這個(gè)元素是一個(gè)樹節(jié)點(diǎn),就往紅黑樹中添加值 else if (p instanceof TreeNode) e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value); else { # 遍歷鏈表 for (int binCount = 0; ; ++binCount) { # 如果當(dāng)前節(jié)點(diǎn)沒有下一節(jié)點(diǎn),就新增尾節(jié)點(diǎn) if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); # 如果鏈表的長(zhǎng)度大于等于8,將鏈表轉(zhuǎn)換為紅黑樹結(jié)構(gòu) if (binCount >= TREEIFY_THRESHOLD – 1) // -1 for 1st treeifyBin(tab, hash); break; } if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } # 判斷是否覆蓋相同key的value if (e != null) { // existing mapping for key V oldValue = e.value; if (!onlyIfAbsent || oldValue == null) e.value = value; afterNodeAccess(e); return oldValue; } } ++modCount; if (++size > threshold) resize(); afterNodeInsertion(evict); return null; }
二、resize()擴(kuò)容源碼
/** * Initializes or doubles table size. If null, allocates in * accord with initial capacity target held in field threshold. * Otherwise, because we are using power-of-two expansion, the * elements from each bin must either stay at same index, or move * with a power of two offset in the new table. * * @return the table */ final Node[] resize() { # 舊的哈希桶數(shù)組 Node[] oldTab = table; # 舊數(shù)組的長(zhǎng)度 int oldCap = (oldTab == null) ? 0 : oldTab.length; # 舊數(shù)組的最大擴(kuò)容限制容量(閾值),如12,24 int oldThr = threshold; int newCap, newThr = 0; # 舊哈希桶數(shù)組長(zhǎng)度大于0時(shí),重新設(shè)置哈希數(shù)組的長(zhǎng)度 if (oldCap > 0) { # 如果超過了默認(rèn)的最大容量1 <= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } # 普通擴(kuò)容 左位移 else if ((newCap = oldCap << 1) = DEFAULT_INITIAL_CAPACITY) newThr = oldThr < 0) // initial capacity was placed in threshold newCap = oldThr; # 默認(rèn)情況下,哈希桶數(shù)組長(zhǎng)度和閾值 else { // zero initial threshold signifies using efaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } # 如果新的閾值等于0,設(shè)置默認(rèn)的數(shù)組容量和閾值 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[] newTab = (Node[])new Node[newCap]; table = newTab; if (oldTab != null) { for (int j = 0; j < oldCap; ++j) { Node e; if ((e = oldTab[j]) != null) { oldTab[j] = null; if (e.next == null) newTab[e.hash & (newCap – 1)] = e; else if (e instanceof TreeNode) ((TreeNode)e).split(this, newTab, j, oldCap); else { // preserve order Node loHead = null, loTail = null; Node hiHead = null, hiTail = null; Node 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; }