基础 | 重写hashCode方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bingbeichen/article/details/83615108

参加美团秋招面试时,被问到 「如何重写hashCode方法?重写hashCode()方法需要注意什么?」,在此做一个系统的总结与梳理,同时也填一下之前埋下的坑,哈哈。


参考答案

为什么要重写hashCode()方法?

Object类中hashCode()方法默认是将对象的存储地址进行映射,并返回一个整形值作为哈希码。

若重写equals()方法,使其比较两个对象的内容,并保留hashCode()方法的默认实现,那么两个明明「相等」的对象,哈希值却可能不同。

如果两个对象通过equals()方法比较的结果为true,那么要保证这两个对象的哈希值相等。

因此,在重写equals()方法时,建议一定要重写hashCode()方法。

如何重写hashCode()方法?

由于Object类的 hashCode() 方法是本地的(native),故其具体实现并不是由Java所完成的。

需要实现hashCode()方法时,可以直接调用Objects.hash(Object… values)方法来获取对应的哈希值。其内部的具体实现是调用Arrays.hashCode(Object[])方法来完成的。

Arrays中计算哈希值的核心代码如下:

public static int hashCode(Object a[]) {
    if (a == null)
        return 0;

    int result = 1;

    for (Object element : a)
        result = 31 * result + (element == null ? 0 : element.hashCode());

    return result;
}

那么,为什么选择数字31作为计算哈希值的因数

The value 31 was chosen because it is an odd prime. If it were even and the multiplication overflowed, information would be lost, as multiplication by 2 is equivalent to shifting. The advantage of using a prime is less clear, but it is traditional. A nice property of 31 is that the multiplication can be replaced by a shift and a subtraction for better performance: 31 * i == (i << 5) - i. Modern VMs do this sort of optimization automatically. ------引用自《Effective Java》

重写hashCode()方法需要注意什么?

  • 应用程序执行期间,只要一个对象用于equals()方法的属性未被修改,则该对象多次返回的哈希值应相等。
  • 如果两个对象通过equals()方法比较的结果为true,那么要保证这两个对象的哈希值相等。
  • 如果两个对象通过equals()方法比较的结果为false,那么这两个对象的哈希值可以相等也可以不相等,但理想情况下是应该不相等,以提高散列表的性能。

源码阅读

// Object类中hashCode()方法的默认实现
public native int hashCode();
// Arrays中计算哈希值的核心代码
public static int hashCode(Object a[]) {
    if (a == null)
        return 0;

    int result = 1;

    for (Object element : a)
        result = 31 * result + (element == null ? 0 : element.hashCode());

    return result;
}

推荐阅读


欢迎关注

Java名企面试吧,每天10点24分,我们不见不散!

丙子先生的宗旨是,每天以短篇幅讲高频面试题,不增加太多负担,但需要持之以恒。

能力有限,欢迎指教!

猜你喜欢

转载自blog.csdn.net/bingbeichen/article/details/83615108