HashMap源码复习之扩容方法

扩容方法

	//为什么需要扩容
	//为了解决哈希冲突导致的链化,影响查询效率的问题,扩容会缓解该问题
    final Node<K,V>[] resize() {
    
		//oldTab:引用扩容前的哈希表
        Node<K,V>[] oldTab = table;
	
		//oldCap:表示扩容之前table数组的长度
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
	
		//oldThr:表示扩容之前的扩容阈值,触发本次扩容的阈值
        int oldThr = threshold;
		
		//newCap:扩容之后table数组的大小
		//newThr:扩容之后,下次再次触发扩容的条件
        int newCap, newThr = 0;
		
		//条件如果成立,说明,hashMap中的散列表已经初始化过了,这是一次正常扩容
        if (oldCap > 0) {
			
			//扩容之前的table数组大小已经达到最大阈值后,则不扩容,且设置扩容条件为int最大值
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
			
			//oldCap左移一位实现数值翻倍,并且赋值给newCap,newCap小于数组最大值限制且扩容之前的阈值>=16
			//这种情况下,则下一次扩容的阈值等于当前阈值翻倍
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                newThr = oldThr << 1; // double threshold
        }
        
		//oldCap == 0,说明hashMap中的散列表是null
		//1.new HashMap(initCap,loadFactor);
		//2.new HashMap(initCap);
		//3.new HashMap(map);并且这个map有数据
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
//oldCap == 0,oldThr == 0
//new HashMap();
else {               // zero initial threshold signifies using defaults
            newCap = DEFAULT_INITIAL_CAPACITY;
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        
		//newThr为零时,通过newCap和loadFactor计算出一个newThr
        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];
        table = newTab;
        
		//说明hashMap本次扩容之前,table不为null
        if (oldTab != null) {
            for (int j = 0; j < oldCap; ++j) {
            
				//当前node节点(链表的头结点,或者为树的根节点)
                Node<K,V> e;

				//说明当前桶位中有数据,但是数据具体是单个数据,还是链表,还是红黑树,并不知道
                if ((e = oldTab[j]) != null) {
                
					//方便JVM GC时回收内存
                    oldTab[j] = null;
                    
					//第一种情况:当前桶位只有一个元素,从未发生过碰撞,这情况下,直接计算出当前元素应存放在新数组的位置,然后扔进去就可以了
                    if (e.next == null)
                        newTab[e.hash & (newCap - 1)] = e;
                        
					//第二种情况:当前节点已经树化
                    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;
                            
							//hash -> .... 1 1111
							//hash -> .... 0 1111
							//0b 10000
                            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;
    }
final Node<K,V> getNode(int hash, Object key) {
		//tab:引用当前hashMap的散列表
		//first:桶位中的头元素
		//e:临时node元素
		//n:table数组长度
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {
            
			//第一种情况:定位出来的桶位元素 即为咱们要get的数据
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))
                return first;
                
			//说明当前桶位不止一个元素,可能是链表也可能是红黑树
            if ((e = first.next) != null) {
				
				//第二种情况:桶位升级成了红黑树
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
				
				//第三种情况:桶位形成链表
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值