JDK8中的HashMap的put key时的源码学习

一、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后存储的哈希桶数组,是一个单向链表数组 Node[] tab; Node p; int n, i; # 如果哈希桶数组是null或者length==0,进行扩容操作,初始化哈希桶数组的容量为16 if ((tab = table) == null || (n = tab.length) == 0) n = (tab = resize()).length; # 如果在取模运算后的索引位置的数组元素是null,直接插入新的链表元素 if ((p = tab[i = (n – 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); # 如果tab[i]的位置是一个长度于等于1的链表,就在最后一个节点next追加新的节点 else { Node e; K k; # 如果已经有相同hash和相同的k if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; # 如果这个元素是一个树节点,就往红黑树中添加值 else if (p instanceof TreeNode) e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value); else { # 遍历链表 for (int binCount = 0; ; ++binCount) { # 如果当前节点没有下一节点,就新增尾节点 if ((e = p.next) == null) { p.next = newNode(hash, key, value, null); # 如果链表的长度大于等于8,将链表转换为红黑树结构 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()扩容源码

/** * 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() { # 旧的哈希桶数组 Node[] oldTab = table; # 旧数组的长度 int oldCap = (oldTab == null) ? 0 : oldTab.length; # 旧数组的最大扩容限制容量(阈值),如12,24 int oldThr = threshold; int newCap, newThr = 0; # 旧哈希桶数组长度大于0时,重新设置哈希数组的长度 if (oldCap > 0) { # 如果超过了默认的最大容量1 <= MAXIMUM_CAPACITY) { threshold = Integer.MAX_VALUE; return oldTab; } # 普通扩容 左位移 else if ((newCap = oldCap << 1) = DEFAULT_INITIAL_CAPACITY) newThr = oldThr < 0) // initial capacity was placed in threshold newCap = oldThr; # 默认情况下,哈希桶数组长度和阈值 else { // zero initial threshold signifies using efaults newCap = DEFAULT_INITIAL_CAPACITY; newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } # 如果新的阈值等于0,设置默认的数组容量和阈值 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; }

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月28日
下一篇 2022年6月28日

相关推荐

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息