putval方法
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { Node<K,V>[] tab; Node<K,V> p; int n, i; //查看是否是第一次put if ((tab = table) == null || (n = tab.length) == 0) //初始化table,n为初始化后table的length n = (tab = resize()).length; //查看是否当个桶子里面有元素,如果没有直接放入 if ((p = tab[i = (n - 1) & hash]) == null) tab[i] = newNode(hash, key, value, null); //如果桶子中有元素 else { Node<K,V> e; K k; //查看第一个元素是否相等,如果相等直接 e = p if (p.hash == hash && ((k = p.key) == key || (key != null && key.equals(k)))) e = p; //如果定位元素是一个TreeNode else if (p instanceof TreeNode) //插入一个TreeNode e = ((TreeNode<K,V>)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); if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st //如果到达一定的条件,进行树化操作 treeifyBin(tab, hash); break; } //如果找到key相等的元素 if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) break; p = e; } } //如果是覆盖,而不是新的插入操作 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) //如果size大过阈值,扩容 resize(); afterNodeInsertion(evict); return null; }
resize函数
final Node<K,V>[] resize() { Node<K,V>[] oldTab = table; //旧的桶子数量 int oldCap = (oldTab == null) ? 0 : oldTab.length; //旧的阈值 int oldThr = threshold; int newCap, newThr = 0; //如果已经初始化过了,里面放有元素 if (oldCap > 0) { //如果hashmap的容量已经达到了桶子数量最大 1 << 30; if (oldCap >= MAXIMUM_CAPACITY) { //扩容到最大 threshold = Integer.MAX_VALUE; //直接返回 return oldTab; } //如果没有达到上限,再次扩大一倍的容量 else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) newThr = oldThr << 1; // double threshold } else if (oldThr > 0) // initial capacity was placed in threshold newCap = oldThr; //还没有初始化过,进行初始化 else { 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; //创建一个新的hash桶子 @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap]; table = newTab; //如果已经被初始化过,但是又来resize的话 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; //把他拿出来,直接放到新的桶子中;e.hash & (newCap - 1)这是一个简单的模运算 //如果桶子的元素是一个树形状的,以后再说,避免过于复杂。 else if (e instanceof TreeNode) ((TreeNode<K,V>)e).split(this, newTab, j, oldCap); //如果是普通的链表,要根据每个hash值,依次放入到新的桶子中 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; }