The moose likes Beginning Java and the fly likes Hashing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Hashing" Watch "Hashing" New topic


sandeep Talari
Ranch Hand

Joined: Dec 24, 2007
Posts: 63
please anybody help me regarding the Hashing process , what is the concept behind the hashmap,hashtable and hashset can anybody help in neat explained manner.Point is how hashmap,hashtable and hashset rely on hashing
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

For fast lookups, objects are stored in so-called buckets. Each bucket contains objects with the exact same hash code.

If the value for a key is looked up, its hash code is calculated first. Then the bucket for that hash code is retrieved. This will decrease the number of objects to search through - instead of checking every single object in the map / set, only the objects in the one bucket will be checked. The better your hash code, the better objects are distributed among buckets, and the faster your map / set becomes.

That also shows the reason why the hash code for equal objects should be the same as well - otherwise, the object will not be found. It also shows another issue: you cannot modify objects stored in the map / set (keys for maps only) in such a way that the hash code will change, or it will never be found again.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Hashing
It's not a secret anymore!