用超类hashCode和Objects覆盖hashCode

问题描述 投票:1回答:1

我是否需要使用super.hashcode()来计算this.hashcode()

IDE(例如IntelliJ Idea)可以生成equals和hashcode。它可以使用java.util.Objects。它也可以覆盖super.hashcode()。

//Immutable class to put it into a hash set.
class Person {

    private final String name;
    // Constructor of not null, getter

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof Person)) return false;
        final Person that = (Person) o;
        return Objects.equals(name, that.name);
    }

    // Auto generated by idea.
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), name);
    }

    @Override
    public String toString() {
        return name;
    }
}

现在让我们有两个同名的实例。他们的代码将是不同的。

public static void main(String[] args) {
    Person person1 = new Person("John");
    Person person2 = new Person("John");

    System.out.println("People are equal: " + person1.equals(person2));
    System.out.println("Person 1: " + person1 + ", Hash code: " + person1.hashCode());
    System.out.println("Person 2: " + person2 + ", Hash code: " + person2.hashCode());

    Set<Person> people = new HashSet<>();
    people.add(person1);
    people.add(person2);

    System.out.println("People: " + people);
}

它打印不同的哈希码。

People are equal: true
Person 1: John, Hash code: -1231047653
Person 2: John, Hash code: -1127452445
People: [John, John]
java hashcode
1个回答
4
投票

在你的例子中,你不应该使用super.hashCode(),因为它将调用Object身份hashCode()。这将打破contract between hashCode() and equals(),根据Object.hashCode() javadoc is

hashCode的一般契约是:

  • 每当在执行Java应用程序期间多次在同一对象上调用它时,hashCode方法必须始终返回相同的整数,前提是不修改对象上的equals比较中使用的信息。从应用程序的一次执行到同一应用程序的另一次执行,该整数不需要保持一致。
  • 如果两个对象根据equals(Object)方法相等,则对两个对象中的每一个调用hashCode方法必须生成相同的整数结果。
  • 如果两个对象根据equals(java.lang.Object)方法不相等,则不需要在两个对象中的每一个上调用hashCode方法必须生成不同的整数结果。但是,程序员应该知道为不等对象生成不同的整数结果可能会提高哈希表的性能。

你必须确保当两个物体是equal()时,它们的hashCode()是相同的。 IntelliJ通过在两种方法中使用相同的字段来确保这一点。

© www.soinside.com 2019 - 2024. All rights reserved.