Recently I read through this Developer Works Document.
The document is all about defining hashCode()
and equals()
effectively and correctly, however I am not able to figure out why we need to override these two methods.
How can I take the decision to implement these methods efficiently?
Assume you have class (A) that aggregates two other (B) (C), and you need to store instances of (A) inside hashtable. Default implementation only allows distinguishing of instances, but not by (B) and (C). So two instances of A could be equal, but default wouldn't allow you to compare them in correct way.
By defining
equals()
andhashCode()
consistently, you can improve the usability of your classes as keys in hash-based collections. As the API doc for hashCode explains: "This method is supported for the benefit of hashtables such as those provided byjava.util.Hashtable
."The best answer to your question about how to implement these methods efficiently is suggesting you to read Chapter 3 of Effective Java.
It is useful when using Value Objects. The following is an excerpt from the Portland Pattern Repository:
hashCode()
:If you only override hash-code method nothing will happen. Because it always return new
hashCode
for each object as an Object class.equals()
:If you only override equal method,
a.equals(b)
is true it means thehashCode
of a and b must be same but not happen. Because you did not overridehashCode
method.Note :
hashCode()
method of Object class always return newhashCode
for each object.So when you need to use your object in the hashing based collection, must override both
equals()
andhashCode()
.hashCode()
method is used to get a unique integer for given object. This integer is used for determining the bucket location, when this object needs to be stored in someHashTable
,HashMap
like data structure. By default, Object’shashCode()
method returns and integer representation of memory address where object is stored.The
hashCode()
method of objects is used when we insert them into aHashTable
,HashMap
orHashSet
. More aboutHashTables
on Wikipedia.org for reference.To insert any entry in map data structure, we need both key and value. If both key and values are user define data types, the
hashCode()
of the key will be determine where to store the object internally. When require to lookup the object from the map also, the hash code of the key will be determine where to search for the object.The hash code only points to a certain "area" (or list, bucket etc) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The
HashTable
then iterates this area (all keys with the same hash code) and uses the key'sequals()
method to find the right key. Once the right key is found, the object stored for that key is returned.So, as we can see, a combination of the
hashCode()
andequals()
methods are used when storing and when looking up objects in aHashTable
.NOTES:
Always use same attributes of an object to generate
hashCode()
andequals()
both. As in our case, we have used employee id.equals()
must be consistent (if the objects are not modified, then it must keep returning the same value).Whenever
a.equals(b)
, thena.hashCode()
must be same asb.hashCode()
.If you override one, then you should override the other.
http://parameshk.blogspot.in/2014/10/examples-of-comparable-comporator.html
In the example below, if you comment out the override for equals or hashcode in the Person class, this code will fail to look up Tom's order. Using the default implementation of hashcode can cause failures in hashtable lookups.
What I have below is a simplified code that pulls up people's order by Person. Person is being used as a key in the hashtable.