HashMap源码-概述

2018-11-25  本文已影响0人  kkyeer

Implementation Notes

常量

内部静态Node类

public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

方法

1.hash方法

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

计算key的hash,结尾是key的hashCode方法返回值(int型)的高16位拼接高16位与低16位按位异或的结果
这么做的原因是:table中计算key对应存储位置的时候,使用的是(capacity-1)&hash,当n=16时,相当于16&hash,也就是,000000000000000001111&hash,也就是前28位确认为0,hash的后四位决定是否碰撞,对于只在高位有区别的key,是大概率会碰撞的,因此将高16位spread到低位去,可以在某些场景减少碰撞,下面是验证:

public static void main(String[] args) {
        Float f1 = 11111.0f;
        Float f2 = 111111.0f;
        int capacity = 16;
        System.out.println("f1的二进制:"+Integer.toBinaryString(f1.hashCode()));
        System.out.println("f2的二进制:"+Integer.toBinaryString(f2.hashCode()));
        System.out.println("HashMap630行,位置计算方式为(n - 1) & hash,假设当前容量为16\n若不进行spread,则:");
        int index1 = (capacity-1)&f1.hashCode();
        int index2 = (capacity-1)&f2.hashCode();
        System.out.println("位置1为:"+index1);
        System.out.println("位置2为:"+index2);
        System.out.println("发生碰撞\n若进行spread:");
        index1 = (capacity-1)&spreadHash(f1.hashCode());
        index2 = (capacity-1)&spreadHash(f2.hashCode());
        System.out.println("位置1为:"+index1);
        System.out.println("位置2为:"+index2);
        System.out.println("不发生碰撞");
    }

    /**
     * 按hashMap的方法,计算spread后的hash
     * @param hash
     * @return
     */
    static  int spreadHash(int hash){
        return hash ^ (hash >>> 16);
    }

f1的二进制:1000110001011011001110000000000
f2的二进制:1000111110110010000001110000000
根据HashMap源码630行,位置计算方式为(n - 1) & hash,假设当前容量为16
若不进行spread,则:
位置1为:0
位置2为:0
发生碰撞
若进行spread:
位置1为:13
位置2为:9
不发生碰撞

为什么是^不是|或者&,验证可得按&是不好的,比如上面例子,按位与的话,还是会碰撞(结果都是0),^和|的运算结果一致,待探究

2.comparableClassFor方法:判断是否实现Comparable接口

static Class<?> comparableClassFor(Object x)

3.compareComparables方法:返回两个Comparable对象的比较值

static int compareComparables(Class<?> kc, Object k, Object x)

4.tableSizeFor:HashMap的方法,计算目标容量对应的2的次方数容量

static final int tableSizeFor(int cap)

内部变量

1.transient Node<K,V>[] table;

核心存储变量,随需要resize,初始化时,容量大小是2的次方

2.transient Set<Map.Entry<K,V>> entrySet;

保存缓存的entrySet

3.transient int size;

存储当前map的大小

4.transient int modCount;

存储当前map发生Structural modifications的次数,用于在迭代时快速失败(fail-fast)

5.int threshold;

下一次resize的阈值,比如当前threshold为16,当前要放17个元素进去,则需要resize

6.final float loadFactor;

负载因子

上一篇下一篇

猜你喜欢

热点阅读