jdk8的HashMap实现

受疫情影响被退隐江湖,赋闲在家,一直996赶太多需求项目和技改了,趁这个机会复习一下java的基础(针对工作中使用的jdk8版本),同时结合这么多年的实战去重新阅读源码。

 

初始化

阿里的开发规约有一条

【推荐】集合初始化时,指定集合初始值大小。

说明:HashMap 使用HashMap(int initialCapacity) 初始化,如果暂时无法确定集合大小,那么指定默 认值(16)即可。

正例:initialCapacity = (需要存储的元素个数 / 负载因子) + 1。注意负载因子(即 loader factor)默认 为 0.75,如果暂时无法确定初始值大小,请设置为 16(即默认值)。

反例:HashMap 需要放置1024 个元素,由于没有设置容量初始大小,随着元素不断增加,容量 7 次被迫 扩大,resize 需要重建 hash 表。当放置的集合元素个数达千万级别时,不断扩容会严重影响性能。

由于CR时会自动标记不符合规约的代码段,因此让提交了没指定HashMap初始值大小的同学注意一下。废话少说,直接看代码。

    /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and load factor.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public HashMap(int initialCapacity, float loadFactor) {
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal initial capacity: " +
                                               initialCapacity);
        if (initialCapacity > MAXIMUM_CAPACITY) // 大于最大值2^30时,赋值为最大值
            initialCapacity = MAXIMUM_CAPACITY;
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
            throw new IllegalArgumentException("Illegal load factor: " +
                                               loadFactor);
        this.loadFactor = loadFactor;
        this.threshold = tableSizeFor(initialCapacity);
    }

    /**
     * Constructs an empty <tt>HashMap</tt> with the specified initial
     * capacity and the default load factor (0.75).
     *
     * @param  initialCapacity the initial capacity.
     * @throws IllegalArgumentException if the initial capacity is negative.
     */
    public HashMap(int initialCapacity) {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
     * (16) and the default load factor (0.75).
     */
    public HashMap() {
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
    }

    /**
     * Returns a power of two size for the given target capacity.
     */
    static final int tableSizeFor(int cap) {
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }

这里的tableSizeFor和记忆中的jdk6不同,举个例子分析一下是如何计算得到大于等于入参的最近一个2的N次方。

cap = 129,129 - 1 = 128,即n = 1000 0000

n |= n >>> 1; // 1000 0000 | 0100 0000 = 1100 0000,将第一个1右移1位,或操作得到“11”,后面低位后续会被覆盖,可以忽略

n |= n >>> 2; // 1100 0000 | 0011 0000 = 1111 0000,将复制后的“11”右移2位,或操作得到“1111”

n |= n >>> 4; // 1111 0000 | 0000 1111 = 1111 1111,将复制后的“1111”右移4位,或操作得到“1111 1111”

……最后+1,即=1 0000 0000=256。

由此可知,其实就是分5次,将入参cap中的二进制第一个1不断进行右移或操作。因为int占4个字节32位,所以最多只需要右移16,就可以保证将高16位的1全部右移到低16位上。最后+1,得到大于(入参-1)的最近一个2的N次方。当入参就是2的N次方时,得到和它相同的值。十分巧妙。

另外,通过代码可见,实例化时(除了HashMap(Map<? extends K, ? extends V> m)、clone())并未申请Node<K,V>[] table,实际是在resize()内部进行延迟初始化的。

    /**
     * The table, initialized on first use, and resized as
     * necessary. When allocated, length is always a power of two.
     * (We also tolerate length zero in some operations to allow
     * bootstrapping mechanics that are currently not needed.)
     */
    transient Node<K,V>[] table;

put

    /**
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
     * to lower.  Because the table uses power-of-two masking, sets of
     * hashes that vary only in bits above the current mask will
     * always collide. (Among known examples are sets of Float keys
     * holding consecutive whole numbers in small tables.)  So we
     * apply a transform that spreads the impact of higher bits
     * downward. There is a tradeoff between speed, utility, and
     * quality of bit-spreading. Because many common sets of hashes
     * are already reasonably distributed (so don't benefit from
     * spreading), and because we use trees to handle large sets of
     * collisions in bins, we just XOR some shifted bits in the
     * cheapest possible way to reduce systematic lossage, as well as
     * to incorporate impact of the highest bits that would otherwise
     * never be used in index calculations because of table bounds.
     */
    static final int hash(Object key) {
        int h;
        // 使用hashCode的高低位异或结果,减少hash碰撞
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

    /**
     * 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 <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    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<K,V>[] tab; Node<K,V> p; int n, i;
        // 首次使用或者延迟实例化,进行resize操作创建Node数组
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        // Node数组中hash对应位置没值,直接新建节点
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            // hash对应位置已有节点
            Node<K,V> e; K k;
            // hash值相同且key相同,准备赋值value
            if (p.hash == hash &&
                ((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);
            // 否则就是链表
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        // 到达链表末尾,添加新node
                        p.next = newNode(hash, key, value, null);
                        // 链表长度大于等于转换为红黑树的阈值时,转换
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash); // node数组长度小于64时,使用resize代替转换红黑树操作
                        break;
                    }
                    // hash值相同且key相同,准备赋值value
                    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;
        // 达到resize阈值时,进行resize操作
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

注意:putVal中的

if ((tab = table) == null || (n = tab.length) == 0)
    n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
    tab[i] = newNode(hash, key, value, null);

由于n总是2的N次方,(n -1) & hash就相当于掩码只取hash的后几位(其实也相当于取模操作,在hash结果平均分布的情况下,(n -1) & hash也是平均分布的),当n<2^16时,就会导致hash的高16位不参与tab的散列运算,如此类推,当n值越少时,参与分表的hash位就越少。因此hash方法才在权衡散列效果和性能后,采用高低16位异或操作得到Node数组的下标。

resize

所有的添加“新”key的操作,只要操作后超过HashMap的resize阈值,都会进行resize操作。或者当尝试从链表转换为红黑树时,如果HashMap的大小小于MIN_TREEIFY_CAPACITY(默认64),那么会用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<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;
            }
            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) {
                    // 旧tab置null,等待gc回收
                    oldTab[j] = null;
                    if (e.next == null) // 同hash只有一个key,直接赋值
                        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;
                        // 按照旧Node数组的大小进行与操作,将数组中的元素按原顺序分散到新Node数组上
                        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;
    }

解析:为什么可以通过判断(e.hash & oldCap) == 0,直接将旧Node数组的元素分散到newTab[j]和newTab[j + oldCap]中。

1.newThr = oldThr << 1; // 新旧大小都是2的N次方,并且 新大小 = 旧大小 * 2

2.e.hash & (newCap - 1) // 数组的定位方式

假设oldCap = 0000 1000,那么旧的定位方式就是 hash & 0111,而新newCap = 0001 0000,那么新的定位方式就是 hash & 1111,可见新旧的差异正是0000 1000,第4位,即oldCap。

当 hash = 0111,hash & (oldCap -1) = 0111,hash & (newCap - 1) = 0111,新旧数组的下标一致,因此可以直接赋值到newTab[j]。

当 hash = 1111,hash & (oldCap -1) = 0111,hash & (newCap - 1) = 1111,新旧数组的下标相差oldCap,因此可以直接赋值到newTab[j + oldCap]。

和putVal的数组下标计算方式保持一致,散列效果好,resize操作也方便。

分树

        /**
         * Splits nodes in a tree bin into lower and upper tree bins,
         * or untreeifies if now too small. Called only from resize;
         * see above discussion about split bits and indices.
         *
         * @param map the map
         * @param tab the table for recording bin heads
         * @param index the index of the table being split
         * @param bit the bit of hash to split on
         */
        final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
            TreeNode<K,V> b = this;
            // Relink into lo and hi lists, preserving order
            TreeNode<K,V> loHead = null, loTail = null;
            TreeNode<K,V> hiHead = null, hiTail = null;
            int lc = 0, hc = 0;
            for (TreeNode<K,V> e = b, next; e != null; e = next) {
                next = (TreeNode<K,V>)e.next;
                e.next = null;
                // 和链表的操作一致,将红黑树的元素划分到高低位两个TreeNode中
                // 这里直接将入参oldCap叫作bit了,明确了作为掩码的用途
                if ((e.hash & bit) == 0) {
                    if ((e.prev = loTail) == null)
                        loHead = e;
                    else
                        loTail.next = e;
                    loTail = e;
                    ++lc;
                }
                else {
                    if ((e.prev = hiTail) == null)
                        hiHead = e;
                    else
                        hiTail.next = e;
                    hiTail = e;
                    ++hc;
                }
            }
            // 处理低位的TreeNode
            if (loHead != null) {
                if (lc <= UNTREEIFY_THRESHOLD) // TreeNode计数小于等于6时,解除红黑树变回链表
                    tab[index] = loHead.untreeify(map);
                else {
                    tab[index] = loHead;
                    if (hiHead != null) // (else is already treeified)
                        loHead.treeify(tab);
                }
            }
            // 处理高位的TreeNode
            if (hiHead != null) {
                if (hc <= UNTREEIFY_THRESHOLD) // TreeNode计数小于等于6时,解除红黑树
                    tab[index + bit] = hiHead.untreeify(map);
                else {
                    tab[index + bit] = hiHead;
                    if (loHead != null) // 转换为红黑树,只需转换一次
                        hiHead.treeify(tab);
                }
            }
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值