IdentityHashMap 源码分析

一、类图

       

二、源码分析

       a.  IdentityHashMap 中判断 key 或 value 是否相等,完全基于引用判断,不使用 equals 判断

       b.  适用于通过引用判断是否相等的场景,如序列化、深拷贝等

       c.  允许 null 类型的 key 或 value

       d.  无序,不保证顺序

       e.  使用线性探测解决冲突,而非链表(与 HashMap 的不同之处),就是一种 hash table

        f.  iterator 是 fail-fast 的,遍历时改变会抛出 ConcurrentModificationException

       g.  实现了序列化和克隆接口

       h.  初始容量 32, 最小容量 4,最大容量 1<<29

       i.   最大大小应该是容量的 2/3,程序写死了

       j.   默认 Object 的 hashcode 方法与 System 的 identityHashCode 是相同的,返回的都是对象内存地址转换为整数值的结果

            IdentityHashMap 是基于 identityHashCode 进行 hash 的,即只与对象内存地址相关

    private static int hash(Object x, int length) {
        int h = System.identityHashCode(x);
        // Multiply by -127, and left-shift to use least bit as part of hash
        return ((h << 1) - (h << 8)) & (length - 1);
    }

     k.  线性探测的原理

          Key Value 交替地存储在数组中, put 后如果发现 3*size>table.length,就扩大为2倍,rehash

           

猜你喜欢

转载自blog.csdn.net/shida_csdn/article/details/81164736
今日推荐