欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JDK 7和JDK8中的HashMap的实现原理不同之处,以及JDK8中HashMap的优势之处

程序员文章站 2022-06-04 19:57:56
...

1、JDK8将数据的存储方式,由数组链表形式,优化为当链表长度大于8的时候,链表形式变为红黑树形式,复杂度由O(n)降至O(logn),提高了查询效率,性能得到了提升

2、扩容方式:

     JDK7: JDK7会传入一个新的更大的容量,并以此创建一个新的Entry数组,然后重新计算hash值将原来的数组元素,拷贝到新的Entry数组中

// JDK7源码
void resize(int newCapacity) {
		//获取原表
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            return;
        }
		//以新容量创建一个新的Entry数组
        Entry[] newTable = new Entry[newCapacity];
        boolean oldAltHashing = useAltHashing;
        useAltHashing |= sun.misc.VM.isBooted() &&
                (newCapacity >= Holder.ALTERNATIVE_HASHING_THRESHOLD);
		//重新计算hash值
        boolean rehash = oldAltHashing ^ useAltHashing;
		//根据新的hash值复制数组
        transfer(newTable, rehash);
        table = newTable;
		//重新计算阈值
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
    }

JDK8:

         扩容时将长度变为原来的2倍newCap = oldCap << 1;

         重新拷贝数组元素时,key<oldTable.length,元素位置不变,如果key > oldTable.length, 元素位置为原来的索引+oldTable.length;

        即如果原来的数组长度为16,元素a,key=5,元素b,key=21,那么在原来的数组中应该在同一个链表中;扩容为32时,元素a仍在位置5,而元素b,应在5+16=21的位置上,节省了重新计算hash的时间,提高了效率

// JDK8源码
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) {
			//判断原容量是否大于最大容量
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }    //否则原容量扩大2倍为新容量
            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 {               // 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];//建立新表
        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;
                    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小于oldCap,小于时,进行与运算值为0
//如二进制oldCap=10000,hash=01101,与运算为0,//否则不为0
                            if ((e.hash & oldCap) == 0) {
                                if (loTail == null)
                                    loHead = e;
                                else
                                    loTail.next = e;
                                loTail = e;
                            }
                            else {//hash大于等于oldCap
                                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;
    }