Results 1 to 10 of 10
 11072012, 11:54 PM #1Member
 Join Date
 Oct 2012
 Posts
 12
 Rep Power
 0
Can someone explain hash table/hash function to me.... Thanks!
Hi, all
I want to get a better understanding of what a hash function is and how it relates to the hash table. I need to implement some sort of word checker that checks a list of words that is read from a text file and stored into the hash table. Then we read from another text file it (containing a couple of sentences some with words that are the same as the ones in the hash table and some are different), if the words that are stored in the hash table do not match the word that is read from the text file sentences, it will output for example, misspellings: word1, word2 .... So it would help if someone can kind of explain this so it will be a bit more clear. Thanks!
 11082012, 10:57 AM #2Moderator
 Join Date
 Apr 2009
 Posts
 12,815
 Rep Power
 22
Re: Can someone explain hash table/hash function to me.... Thanks!
Wiki hash tables.
The hashCode function (in Java) simply maps an object into an int, it's that int that is used by the HashMap/Table/Set for its "buckets".Please do not ask for code as refusal often offends.
** This space for rent **
 11082012, 11:24 AM #3Member
 Join Date
 Oct 2012
 Posts
 12
 Rep Power
 0
Re: Can someone explain hash table/hash function to me.... Thanks!
Oh okay so letter like ABC have int values associated with them? that can be mapped into a hash table? If so how do we associate the letters to ints?
 11082012, 11:59 AM #4Moderator
 Join Date
 Apr 2009
 Posts
 12,815
 Rep Power
 22
Re: Can someone explain hash table/hash function to me.... Thanks!
The String class already has a hashCode method.
It looks like this:
Java Code:public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; }
Please do not ask for code as refusal often offends.
** This space for rent **
 11082012, 12:13 PM #5Member
 Join Date
 Oct 2012
 Posts
 12
 Rep Power
 0
Re: Can someone explain hash table/hash function to me.... Thanks!
can you kind of explain the method above what it is doing thanks!
 11082012, 01:32 PM #6Moderator
 Join Date
 Apr 2009
 Posts
 12,815
 Rep Power
 22
Re: Can someone explain hash table/hash function to me.... Thanks!
It's turning a String into an int, based on the value of each of its chars.
Java Code:h = 31*h + val[off++];
Please do not ask for code as refusal often offends.
** This space for rent **
 11112012, 11:18 AM #7Member
 Join Date
 Oct 2012
 Posts
 12
 Rep Power
 0
Re: Can someone explain hash table/hash function to me.... Thanks!
How would i represent a string in terms of int values, and also in that code what the offset mean?
 11112012, 01:50 PM #8Moderator
 Join Date
 Apr 2009
 Posts
 12,815
 Rep Power
 22
Re: Can someone explain hash table/hash function to me.... Thanks!
You don't really need to know that sort of detail.
All you need to know is that a hashcode is an integer representation of the object in question.
Since String already has this done then I'm not sure what you are looking for.Please do not ask for code as refusal often offends.
** This space for rent **
 11112012, 02:28 PM #9Member
 Join Date
 Oct 2012
 Posts
 12
 Rep Power
 0
Re: Can someone explain hash table/hash function to me.... Thanks!
well we are suppose to implement our own hash function rather than using the one from the java library itself. So that is why I want to understand it..
 11122012, 12:38 PM #10Moderator
 Join Date
 Apr 2009
 Posts
 12,815
 Rep Power
 22
Re: Can someone explain hash table/hash function to me.... Thanks!
In that case you need to read up on it, as I suggested above.
Wiki is a reasonable start point.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads

help regarding distributed hash table
By priya31 in forum NetworkingReplies: 0Last Post: 04062011, 05:33 PM 
hash table
By pinkfahtema in forum New To JavaReplies: 1Last Post: 03282011, 08:25 AM 
I need a 32 or 64 bit hash function
By fogus in forum New To JavaReplies: 12Last Post: 03182009, 03:52 AM 
Hash Table Help
By michael_mke in forum New To JavaReplies: 3Last Post: 11272008, 06:12 PM 
Hash Table help
By rhm54 in forum New To JavaReplies: 0Last Post: 02082008, 02:25 AM
Bookmarks