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?
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).
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.
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.
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.
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.
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.
hashCode
(and equals
) override is based on a single member of your class, use Objects.hashCode( member )
. hashCode
(and equals
) override is based on multiple attribute of your class, use Objects.hash( memberA , memberB , memberC )
. @Override
public int hashCode() {
return this.member.hashCode() ; // Throws NullPointerException if member variable is null.
}
@Override
public int hashCode() {
return Objects.hashCode( this.member ) ; // Returns zero (0) if `this.member` is NULL, rather than throwing exception.
}
@Override
public int hashCode() {
return Objects.hash( this.memberA , this.memberB , this.memberC ) ; // Hashes the result of all the passed objects’ individual hash codes.
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With