Saturday, August 16, 2014

What is the use of hashcode in Java?

Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements of the set/map. this bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to... if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not.

http://stackoverflow.com/questions/3563847/what-is-the-use-of-hashcode-in-java

No comments:

Post a Comment