Results 1 to 10 of 10
- 11-07-2012, 10:54 PM #1
Member
- 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!
- 11-08-2012, 09:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 11-08-2012, 10:24 AM #3
Member
- 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?
- 11-08-2012, 10:59 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Can someone explain hash table/hash function to me.... Thanks!
The String class already has a hashCode method.
It looks like this:
Slightly random indenting can be blamed on the source code files.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.
- 11-08-2012, 11:13 AM #5
Member
- 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!
- 11-08-2012, 12:32 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
You'll need to look into the technical aspects of hashing for why exactly it does it this way.Java Code:h = 31*h + val[off++];
Please do not ask for code as refusal often offends.
- 11-11-2012, 10:18 AM #7
Member
- 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?
- 11-11-2012, 12:50 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
- 11-11-2012, 01:28 PM #9
Member
- 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..
- 11-12-2012, 11:38 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
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.
Similar Threads
-
help regarding distributed hash table
By priya31 in forum NetworkingReplies: 0Last Post: 04-06-2011, 05:33 PM -
hash table
By pinkfahtema in forum New To JavaReplies: 1Last Post: 03-28-2011, 08:25 AM -
I need a 32 or 64 bit hash function
By fogus in forum New To JavaReplies: 12Last Post: 03-18-2009, 02:52 AM -
Hash Table Help
By michael_mke in forum New To JavaReplies: 3Last Post: 11-27-2008, 05:12 PM -
Hash Table help
By rhm54 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks