Results 1 to 12 of 12
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default understanding of hash functions

    I read about hash table and Hash functions on wikipedia, becuase I have to use HashTable in a code, but I can't understand the Hash functions quite properly, now from what I read, it said its like a map with each key having several values, is it correct?
    What about HashSet in the Java Docs, it said its backed by a HashMap, but what's it use?
    Thanks

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: understanding of hash functions

    Do you really need to fully understand hash functions in order to use Hashtable?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: understanding of hash functions

    The fact that a HashSet is implemented/backed by a HashMap is an 'implementation detail' and is none of our business as long as the HashSet implements the Set interface.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: understanding of hash functions

    No, but I want to know how it works, to better understand it.

    But is this part correct:
    its like a map with each key having several values, is it correct?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: understanding of hash functions

    One key in a HashMap can only have one value associated with it and all keys have to be unique according to their equals( ... ) method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: understanding of hash functions

    Quote Originally Posted by JosAH View Post
    One key in a HashMap can only have one value associated with it and all keys have to be unique according to their equals( ... ) method.

    kind regards,

    Jos
    But isnt this is how a normal map works, each key has a unique value?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: understanding of hash functions

    Quote Originally Posted by aizen92 View Post
    But isnt this is how a normal map works, each key has a unique value?
    Yep, but you mentioned "several values" per key in reply #4, so I replied to that and the values needn't be unique, only the keys need to be unique in the Map.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: understanding of hash functions

    Ohhh, okay
    But I'm getting a little confused here lol
    Do you mean that in a map values are not unique but in hash map they are unique?
    Sorry for the long discussion but I want to grasp a better understanding of it

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: understanding of hash functions

    Quote Originally Posted by aizen92 View Post
    Ohhh, okay
    But I'm getting a little confused here lol
    Do you mean that in a map values are not unique but in hash map they are unique?
    Sorry for the long discussion but I want to grasp a better understanding of it
    No, according to the contract of the Map interface, each Map can store unique keys only. Values are free to take any value they want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

    Default Re: understanding of hash functions

    Then what's the use of Hash here?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: understanding of hash functions

    Quote Originally Posted by aizen92 View Post
    Then what's the use of Hash here?
    Hashing is a (fast) way to find an integer value for any type of object that is mostly unique. Such a hash value can be used in hash tables/maps to store and find those key values.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    4

Similar Threads

  1. Hash set
    By lakshmibvaraprasad in forum New To Java
    Replies: 2
    Last Post: 07-20-2011, 06:58 PM
  2. Hash-tables IO
    By loopsnhoops in forum New To Java
    Replies: 8
    Last Post: 06-01-2011, 09:55 AM
  3. Having trouble understanding how to use Hash Tables
    By theBurgh22 in forum Advanced Java
    Replies: 1
    Last Post: 03-04-2011, 11:19 PM
  4. Hash
    By sandy1028 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 10:36 AM
  5. Hash Map
    By rekha in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 01:00 PM

Posting Permissions

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