Today let's talk about equals and hashcode.
We know equal is used to verify the equality of two objects. So the default implementation of equal checks if the two objects are in the same memory address and if it is so, it will come out with value equal to true. This is good and this works well.
So, there would be scenarios when you would require your own custom implementation of equals method. For example, let's consider we have a person class. The person class has name as one of its property. Now, if there are two objects that you would like to check for equality, we will say the objects are equal if and only if they have the same name. So in this case what you will do is you will implement custom implementation of equals method. What essentially we are saying is that male is the identity of the person object. It also means if your story person object we would like to never where there are two person objects with the same name. Because essentially at the end of the day it is one person object.
Before we look at the contract between hashcode and equals let's understand what is a dashboard method. Hashcode returns a unique integer value for the object in runtime by default the integer value is derived from memory address of the object in heap. So in a way, though not mandatory hashcode is kind of a location or a pointer to the address of the object in memory, directly or indirectly.
Now let's try to link the two methods, hashcode and equals using our person object as an example.
Let's say, we created a person object name equal to "A". We stored this object somewhere in Memory. Forget about the data structure where we are storing this for a moment. Now let's say we create it another person object with name equal to "A". If we attempt to store this in memory ideally it should be stored at the same location as previous person object because from our perspective it is the same object or equal object.
You may get confused because I am linking hashcode directly to the memory address. But, having said that, there are data structures in Java that rely on hashcode of the object to decide the memory location of the object. A very good example is Hashmap. Hashmap decides bucket where it will store the object based on hashcode of the object. Bucket it is decided based on sub computation around hashcode of the object.
So, Coming back to our example of two person objects, we would like to store then in the same memory location at-least if not treated as the same runtime object. Now to achieve this since Java depends on hashcode, if you are overriding equals implementation, you will need to override hashcode implementation.
Apache common provides utility methods around implementations for both equals and hashcode. The classes you can use are Hashcodebuilder and Equalbuilder.
See below for implementation of hashcode and equals.
@Override
public int hashCode() {
return new HashCodeBuilder().append(id).append(name).toHashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof Person)) {
return false;
}
Person other = (Person) obj;
return new EqualsBuilder().append(this.id, other.id)
.append(this.name, other.name).isEquals();
}
No comments:
Post a Comment