HashMap源码解析 - 1835434698/1835434698.github.io GitHub Wiki

【HashMap源码解析

//构造方法
public HashMap() {
    this.loadFactor = DEFAULT_LOAD_FACTOR; // 加载因子
}
public HashMap(int initialCapacity) {
    this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
public HashMap(int initialCapacity, float loadFactor) {
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal initial capacity: " +
                                           initialCapacity);
    if (initialCapacity > MAXIMUM_CAPACITY)
        initialCapacity = MAXIMUM_CAPACITY;
    if (loadFactor <= 0 || Float.isNaN(loadFactor))
        throw new IllegalArgumentException("Illegal load factor: " +
                                           loadFactor);
    this.loadFactor = loadFactor;
    this.threshold = tableSizeFor(initialCapacity);
}
//直接传递一个map作为构造参数。
public HashMap(Map<? extends K, ? extends V> m) {
    this.loadFactor = DEFAULT_LOAD_FACTOR;
    putMapEntries(m, false);//调用 01
}
//01
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
        int s = m.size();
        if (s > 0) {
            if (table == null) { // pre-size
                float ft = ((float)s / loadFactor) + 1.0F;//需要扩展的次数
                int t = ((ft < (float)MAXIMUM_CAPACITY) ?
                         (int)ft : MAXIMUM_CAPACITY);//判断是否大于最大值
                if (t > threshold)
                    threshold = tableSizeFor(t);//得到最近的2的整数次幂的数
            }
            else if (s > threshold)
                resize();//重新计算大小 跳转 02
            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {//遍历
                K key = e.getKey();
                V value = e.getValue();
                //首先计算hash,然后存储到新对象 跳转 03,然后04
                putVal(hash(key), key, value, false, evict);
            }
        }
    }
//02
//初始化或者扩容之后元素调整
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) {   // 如果原table不为空
            // 如果数组长度达到最大值,则修改临界值为Integer.MAX_VALUE
            if (oldCap >= MAXIMUM_CAPACITY) {
                threshold = Integer.MAX_VALUE;
                return oldTab;
            }
            // 下面就是扩容操作(2倍)
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
                // threshold也变为二倍
                newThr = oldThr << 1;
        }
        else if (oldThr > 0) // initial capacity was placed in threshold
            newCap = oldThr;
        else {               // threshold为0,则使用默认值
            newCap = DEFAULT_INITIAL_CAPACITY;  
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
        }
        if (newThr == 0) {  // 如果临界值还为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;
                            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;
    }

//03
static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
存值哈希表hashtable(keyvalue) 就是把Key通过一个固定的算法函数既所谓的哈希函数转换成一个整型数字然后就将该数字对数组长度进行取余取余结果就当作数组的下标将value存储在以该数字为下标的数组空间里//04
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;//tab就算hashmap的数组。 p为数组的一个节点也是一个链表的端点。
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;//数组为空,重新计算。
        if ((p = tab[i = (n - 1) & hash]) == null)//除法散列法进行散列(数组层次为空一定不会碰撞)。取出应该存入的链表的端点。如果链表为空,进入if内
            tab[i] = newNode(hash, key, value, null);//创建node对象,并且放入链表端点。
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&//与端点的hash对比,hashcode相等,然后使用下一个判断
                ((k = p.key) == key || (key != null && key.equals(k))))//判断是否是同一个对象
                e = p;
            else if (p instanceof TreeNode)//如果是红黑树结点的话,进行红黑树插入
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);//跳转 05
            else {//不是红黑树,是长度小于8的链表
                for (int binCount = 0; ; ++binCount) {//遍历判断在链表中哪一个有碰撞
                    if ((e = p.next) == null) {//遍历链表,如果是到了链表尾部,则创建新节点
                        p.next = newNode(hash, key, value, null);//则创建新节点
                        if (binCount >= TREEIFY_THRESHOLD - 1) // 判断长度是否大于等于8,
                            treeifyBin(tab, hash);//转换为树 跳转 06
                        break;
                    }
                    //遍历 判断是否是同一个对象
                    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)
            resize();
        afterNodeInsertion(evict);
        return null;
    }
//05
final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
                               int h, K k, V v) {
    Class<?> kc = null;
    boolean searched = false;
    TreeNode<K,V> root = (parent != null) ? root() : this;// 判断是否有根节点,没有则设置自己。
    for (TreeNode<K,V> p = root;;) {// 遍历树
        int dir, ph; K pk;
        if ((ph = p.hash) > h)// 判断hash值大小,决定放在左子树还是右子树
            dir = -1;
        else if (ph < h)
            dir = 1;
        else if ((pk = p.key) == k || (k != null && k.equals(pk)))//是同一个
            return p;
        else if ((kc == null &&
                  (kc = comparableClassFor(k)) == null) ||
                 (dir = compareComparables(kc, k, pk)) == 0) {
            if (!searched) {
                TreeNode<K,V> q, ch;
                searched = true;
                if (((ch = p.left) != null &&
                     (q = ch.find(h, k, kc)) != null) ||
                    ((ch = p.right) != null &&
                     (q = ch.find(h, k, kc)) != null))
                    return q;
            }
            dir = tieBreakOrder(k, pk);//用类名转ascii作对比
        }

        TreeNode<K,V> xp = p;
        if ((p = (dir <= 0) ? p.left : p.right) == null) {
            Node<K,V> xpn = xp.next;
            TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
            if (dir <= 0)
                xp.left = x;
            else
                xp.right = x;
            xp.next = x;
            x.parent = x.prev = xp;
            if (xpn != null)
                ((TreeNode<K,V>)xpn).prev = x;
            moveRootToFront(tab, balanceInsertion(root, x));//重新平衡树
            return null;
        }
    }
}
//06
final void treeifyBin(Node<K,V>[] tab, int hash) {
    int n, index; Node<K,V> e;
    if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
        resize();
    else if ((e = tab[index = (n - 1) & hash]) != null) {
        TreeNode<K,V> hd = null, tl = null;
        do {
            TreeNode<K,V> p = replacementTreeNode(e, null);//转换为树节点。
            if (tl == null)
                hd = p;
            else {
                p.prev = tl;
                tl.next = p;
            }
            tl = p;
        } while ((e = e.next) != null);
        if ((tab[index] = hd) != null)
            hd.treeify(tab);//转为真正的平衡树二叉树。跳转07
    }
}
//07
   final void treeify(Node<K,V>[] tab) {
            TreeNode<K,V> root = null;
            for (TreeNode<K,V> x = this, next; x != null; x = next) {
                next = (TreeNode<K,V>)x.next;
                x.left = x.right = null;//树的左右孩子置空
                if (root == null) {//根
                    x.parent = null;
                    x.red = false;//黑色
                    root = x;
                }
                else {
                    K k = x.key;
                    int h = x.hash;
                    Class<?> kc = null;
                    for (TreeNode<K,V> p = root;;) {//遍历树
                        int dir, ph;
                        K pk = p.key;
                        if ((ph = p.hash) > h)//对比hash值,决定左子树还是右子树
                            dir = -1;
                        else if (ph < h)
                            dir = 1;
                        else if ((kc == null &&
                                  (kc = comparableClassFor(k)) == null) ||
                                 (dir = compareComparables(kc, k, pk)) == 0)
                            dir = tieBreakOrder(k, pk);//ascii对比、identityHashCode对比

                        TreeNode<K,V> xp = p;
                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
                            x.parent = xp;
                            if (dir <= 0)
                                xp.left = x;
                            else
                                xp.right = x;
                            root = balanceInsertion(root, x);//平衡树
                            break;
                        }
                    }
                }
            }
            moveRootToFront(tab, root);
        }
⚠️ **GitHub.com Fallback** ⚠️