This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashing

 
sandeep Talari
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic