Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Automatic equals() and hashCode()

Tags:

Implementing equals() and hashCode() for simple data POJOs is cluttering my code and maintaining is tedious.

What are the libraries handling this automatically?
I prefer bytecode instrumentation over AOP approach due to performance reasons.

Update: Topic of necessity of implementing equals() and hashCode() has been discussed, here's my point:

Isn't it better to have it done right upfront with minimal effort rather than digging in the code, adding hC/eq when it comes to it?

like image 435
Ondra Žižka Avatar asked Aug 05 '11 16:08

Ondra Žižka


People also ask

What is the relationship between hashCode () and equals () method in Java?

If two objects are equal(according to equals() method) then the hashCode() method should return the same integer value for both the objects. But, it is not necessary that the hashCode() method will return the distinct result for the objects that are not equal (according to equals() method).

What is the hashCode () and equals () function?

Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.

How hashCode () and equals () method are used in HashMap?

In HashMap, hashCode() is used to calculate the bucket and therefore calculate the index. equals() method: This method is used to check whether 2 objects are equal or not. This method is provided by the Object class. You can override this in your class to provide your implementation.

What is difference between hashCode and equals in Java?

The value returned by hashCode() is the object's hash code, which is the object's memory address in hexadecimal. equals() checks if the two object references are same. If two objects are equal then their hashCode must be the same, but the reverse is not true.


2 Answers

Project Lombok provides the annotation @EqualsAndHashCode which will generate equals() and hashCode() for your Java classes. Of course there are some drawbacks in comparison to manually implementing these methods, so make sure you read the "small print" on the linked page.

like image 200
joschi Avatar answered Sep 19 '22 09:09

joschi


Java 7 & later

While not the panacea you requested, writing the hashCode override is a bit easier now as of Java 7 and later.

Objects.hashCode & Objects.hash

As of Java 7, the Objects class offers a couple of utility methods for generating hash code values.

See my Answer on a related Question for more discussion.

  • If your hashCode (and equals) override is based on a single member of your class, use Objects.hashCode( member ).
  • If your hashCode (and equals) override is based on multiple attribute of your class, use Objects.hash( memberA , memberB , memberC ).

Single member, not tolerating a NULL

@Override
public int hashCode() {
    return this.member.hashCode() ;  // Throws NullPointerException if member variable is null.
}

Single member, tolerating a NULL

@Override
public int hashCode() {
    return Objects.hashCode( this.member ) ;  // Returns zero (0) if `this.member` is NULL, rather than throwing exception.
}

Multi-member

@Override
public int hashCode() {
    return Objects.hash( this.memberA , this.memberB , this.memberC  ) ;  // Hashes the result of all the passed objects’ individual hash codes.  
}
like image 34
Basil Bourque Avatar answered Sep 20 '22 09:09

Basil Bourque