This content has been marked as final. Show 4 replies
AnjanN wrote:By making sure that equal objects return true from the equals() method (and non-equal objects return false).
How can we use object as key in a collection by overriding the hashcode() and equals() method.
And that the generated hashcode() is the same for 2 equal objects (it can be the same for non-equal objects too).
I cannot understand the real logical picture behind that.Well, perhaps you should Google around for an explanation.
The short version is: the hashcode determines a "bucket". In the bucket, the equals() method is used to find the correct key.
AnjanN wrote:Easily. You can implement hashcode() as "return 1;", it will just perform poorly and completely bypass the idea of hashing in the first place, but it will work.
it can be the same for non-equal objects too
how can that be possible that two different objects having same hashcodes..
It's the equals() method that's used to see if 2 keys are equal.