java hashmap

hashmap结构

数组下标计算

下标 = hash(key) \% length
当数组长度正好是 2的n次方时。
下标 = hash(key) \% length =(length - 1) \& hash(key)
即,当数组长度正好是 2的n次方时,%运算可以转换为 & 运算,效率更高。

hash函数

    static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
    }

hash冲突

hash(key1) = hash(key2)
当key1和key2 hash值相等时(hash冲突),导致下标一样,这时hashmap采用尾插法将key2插入到key1的尾部

image.png

put流程

image.png

hashmap链表转红黑树时机

 if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
//链表长度大于等于 TREEIFY_THRESHOLD
                            treeifyBin(tab, hash);
                        break;
    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)
//桶数组容量大于等于 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);
        }
    }

参考:
https://tech.meituan.com/2016/06/24/java-hashmap.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容