关于hashCode

💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤💤


hashCode的定义

首先要明白的是:hashCode是定义在Object类中的一个方法,通过调用该方法可以获取对象的散列码。

hashCode的常规约定

在 Java 应用程序执行期间,在同一对象上多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是对象上 equals 比较中所用的信息没有被修改。

从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。 

如果根据 equals(Object) 方法,两个对象是相等的,那么在两个对象中的每个对象上调用 hashCode 方法都必须生成相同的整数结果。 

以下情况不是必需的:如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么在两个对象中的任一对象上调用 hashCode 方法必定会生成不同的整数结果。但是,程序员应该知道,为不相等的对象生成不同整数结果可以提高哈希表的性能。 

当equals方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。

hashCode的实现

打开Object的源码,可以看到一下内容:

     /* <p>
     * As much as is reasonably practical, the hashCode method defined by
     * class {@code Object} does return distinct integers for distinct
     * objects. (This is typically implemented by converting the internal
     * address of the object into an integer, but this implementation
     * technique is not required by the
     * Java&trade; programming language.)
     * @return  a hash code value for this object.
     * @see     java.lang.Object#equals(java.lang.Object)
     * @see     java.lang.System#identityHashCode
     */
    public native int hashCode();

实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 JavaTM 编程语言不需要这种实现技巧。) 

hashCode是由native修饰的本地方法,在Object类中没有实现。

如果想知道继续知道hashCode的实现,就需要对OpenJDK8源码的进行分析

由于对C++语法尚不掌握,所以直接说结论:

OpenJDK8 默认hashCode的计算方法是通过和当前线程有关的一个随机数三个确定值,运用Marsaglia's xorshift scheme随机数算法得到的一个随机数。

String的hashCode方法

    /**
     * Returns a hash code for this string. The hash code for a
     * {@code String} object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using {@code int} arithmetic, where {@code s[i]} is the
     * <i>i</i>th character of the string, {@code n} is the length of
     * the string, and {@code ^} indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
        // 初次调用hash默认值为0
        int h = hash;
        // 这里检查是否初次调用
        if (h == 0 && value.length > 0) {
            // value是char类型的数组,里面保存着String的字符
            char val[] = value;
            // 计算hashCode的值
            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            // 赋值给hash,下次调用直接返回结果
            hash = h;
        }
        return h;
    }

 String类计算hashCode是通过以下算法实现:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

s[0]为字符的ASCII值

^ 表示求幂

测试:

        String s = "a";
        System.out.println(s.hashCode());
输出:97
即为字符a的ASCII值
        String s = "ab";
        System.out.println(s.hashCode());
输出: 3105
        int m = (int)(Math.pow(31,1)*97+98);
        System.out.println(m);
输出: 3105

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值