·

直接把 HashMap 计算桶位置的代码拿出来看

public class HashSetTest {
    public static void main(String[] args) {
        int h;
        Dog dog1 = new Dog("tom");
        System.out.println(((h = dog1.hashCode()) ^ (h >>> 16)) & 15);
        Dog dog2 = new Dog("tom");
        System.out.println(((h = dog2.hashCode()) ^ (h >>> 16)) & 15);
        Dog dog3 = new Dog("tom");
        System.out.println(((h = dog3.hashCode()) ^ (h >>> 16)) & 15);
    }
}

class Dog {
    private String name;

    public Dog(String name) {
        this.name = name;
    }
}

我试了一下,直接跑跟 debug 的结果确实不一样,换不同的 JDK 也会不一样