简体   繁体   English

用超类hashCode和Object覆盖hashCode

[英]Override hashCode with super class hashCode and Objects

Do I need to use super.hashcode() to calculate this.hashcode() ? 我需要使用super.hashcode()来计算this.hashcode()吗?

IDE (IntelliJ Idea for example) can generate equals and hashcode. IDE(例如,IntelliJ Idea)可以生成等于和哈希码。 It can use java.util.Objects . 它可以使用java.util.Objects It can also override super.hashcode(). 它还可以覆盖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;
    }
}

Now let's have two instances with the same name. 现在,我们有两个名称相同的实例。 Their hascodes will be different. 他们的hascode将有所不同。

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);
}

It prints different hashcodes. 它输出不同的哈希码。

People are equal: true
Person 1: John, Hash code: -1231047653
Person 2: John, Hash code: -1127452445
People: [John, John]

In your example you shouldn't use super.hashCode() as it will call the Object identity hashCode() . 在您的示例中,您不应该使用super.hashCode() ,因为它将调用Object标识hashCode() This would break the contract between hashCode() and equals() , which as per Object.hashCode() javadoc is 这将破坏hashCode()equals()之间协定 ,根据Object.hashCode() javadoc

The general contract of hashCode is: hashCode的一般约定为:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. 只要在Java应用程序执行期间在同一对象上多次调用它,hashCode方法就必须始终返回相同的整数,前提是不修改该对象的equals比较中使用的信息。 This integer need not remain consistent from one execution of an application to another execution of the same application. 从一个应用程序的执行到同一应用程序的另一执行,此整数不必保持一致。
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 如果根据equals(Object)方法,两个对象相等,则在两个对象中的每个对象上调用hashCode方法必须产生相同的整数结果。
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. 根据equals(java.lang.Object)方法,如果两个对象不相等,则不需要在两个对象中的每个对象上调用hashCode方法必须产生不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 但是,程序员应该意识到,为不相等的对象生成不同的整数结果可能会提高哈希表的性能。

You must ensure that when two objects are equal() their hashCode() is the same. 您必须确保当两个对象equal()它们的hashCode()相同。 IntelliJ ensures this by using the same fields in both methods. IntelliJ通过在两种方法中使用相同的字段来确保这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM