Results 1 to 10 of 10
  1. #1
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default 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!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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 **

  3. #3
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default 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?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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;
        }
    Slightly random indenting can be blamed on the source code files.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Can someone explain hash table/hash function to me.... Thanks!

    can you kind of explain the method above what it is doing thanks!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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++];
    You'll need to look into the technical aspects of hashing for why exactly it does it this way.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default 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?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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 **

  9. #9
    kkmoslehpour is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default 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..

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default 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

  1. help regarding distributed hash table
    By priya31 in forum Networking
    Replies: 0
    Last Post: 04-06-2011, 06:33 PM
  2. hash table
    By pinkfahtema in forum New To Java
    Replies: 1
    Last Post: 03-28-2011, 09:25 AM
  3. I need a 32 or 64 bit hash function
    By fogus in forum New To Java
    Replies: 12
    Last Post: 03-18-2009, 03:52 AM
  4. Hash Table Help
    By michael_mke in forum New To Java
    Replies: 3
    Last Post: 11-27-2008, 06:12 PM
  5. Hash Table help
    By rhm54 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •