Equal Objects must have equal hashcodes?
Equal Objects must have equal hashcodes. As per my understanding this statement is valid when we have intention of using object in hashbased datastuctures. This is one of contract for hashcode and equals method in java docs. I explored the reason why this is said and looked in the implementation of hashtable and found out below code in put
method
if ((e.hash == hash) && e.key.equals(key))
So I got it, contract came from condition e.hash == hash
above. I further tried to explore why java is checking hashcode when comparing two objects for equality. So here is my understaing
If two equal object have equal hascodes then they can be stored in the same bucket and this will be good in terms of look up in single bucket only
Its better to check hashcode then actually calling equals method because hascode method is les开发者_开发百科s costly than equals method, because here we just have to compare int value where in equals method may be invloving object field comparison. So hashcode method providing one extra filter.
Please correct me if both above reasons are valid?
- Correct, just a small correction - if two unequal objects have the same hashcode.
- Not exactly, It's better to check it first, as a filter for the non-equal, but if you want to make sure the objects are equal, you should call
equals()
You got it wrong. equals
just returns a boolean value (two possible values), and needs another object to compare against. hashCode
returns an int (2^32 possible values), and only needs the object to be called.
The HashMap
tries to distribute all the objects it holds among buckets. When put
is called on the map, it has to decide which bucket it will use for the given object. It thus uses hashCode
(modulo the number of buckets) to decide which bucket to use. Then, once the bucket is found, it has to check whether the key is already in the map or not. To do this, it compares every object in the bucket with the object to put in the map. And to do this, it uses equals
. If the object isn't found, it adds it in the bucket.
hashCode
isn't used because it's faster than equals
. It's used because it allows distributing keys among a set of buckets. And it's much faster to compute the hashCode once and compare the object with (hopefully) 0, one or two objects in the same bucket that to compare the object with the thousands of objects already stored in the map.
" I further tried to Exlpore why java is checking Hashcode when comparing two objects for equality". Put method is not just checking for equality, it is trying to first narrow down the bucket and then use the equals. That is why we need to combine HashCode with Equals in case of bucketed collections.
But if your sole intention is to just check equality between two objects, you will never need a hashcode method.
Obj1.equals(Obj2) will never use the hashcode method by default.
Its a general type of contract so that when we store the objects inside a hashing based data structure, then we should always consistently put or get the same object to and from the hashtable. Its a contract which we have created to be followed such that the entry/put processes occur smoothly.
精彩评论