1

Do I need to use super.hashcode() to calculate this.hashcode()?

IDE (IntelliJ Idea for example) can generate equals and hashcode. It can use java.util.Objects. It can also override 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.

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]
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114

1 Answers1

4

In your example you shouldn't use super.hashCode() as it will call the Object identity hashCode(). This would break the contract between hashCode() and equals(), which as per Object.hashCode() javadoc is

The general contract of hashCode is:

  • 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. 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.
  • 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. 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. IntelliJ ensures this by using the same fields in both methods.

Community
  • 1
  • 1
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • thanks for binning links. IntelliJ ensures this by using the same fields in both methods. - people are equal and their hashes are not! – Yan Khonski Mar 29 '19 at 16:05
  • IntelliJ 2018.3.5 doesn't use `super.hashCode()` when generating `hashCode()` method. If objects are `equal()` but `hashCode()` is different than there is a bug in `hashCode()` implementation. It will break `HashMap` and other structures that depend on `hashCode()`. – Karol Dowbecki Mar 29 '19 at 20:47