Reputation: 324
Lets imagine we have data we want to put in an Hashtable. The Hashfunction calculates an Hashvalue for each data object and put this hashvalues into a table (each value should get its own bucket). With the hashvalue we know the exact position of the data object in the table.
What role does the key play here? The HashMap in Java wants a specific key for every value we put into the HashMap and with the key we can get the value.
I am wondering whats the difference between the value we want to put into an Hashtable (in java Hashmap), the hashvalue and the key? What is the mathematic behind that?
Upvotes: 2
Views: 4117
Reputation: 1500504
You always need to original key, to cope with hash collisions. The point of the hash code (or hash value as you're calling it) is to be able to very quickly find possible matches for the keys. The hash code is solely based on the key - the value is completely irrelevant.
So logically, a fetch from a hash table is:
null
to indicate that result.(The exact way in which the hash table is divided into buckets is an implementation detail. Sometimes each bucket only contains a single entry but can be chained to other buckets; in other cases a bucket can contain several entries. See the wikipedia entry on hash tables for more information.)
Here an "entry" is a {key, value, hash}
tuple:
Upvotes: 4