10 Replies - 1389 Views - Last Post: 07 April 2010 - 10:59 PM Rate Topic: -----

#1 mattlyons   User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

HashTable

Posted 07 April 2010 - 07:40 PM

Got a test tomorrow and this is on the study guide. The question is:

"Define a table size and a reasonable hash function for the following problem. We need to store and retrieve Human Resource objects for up to 1000 employees using the SSN (nine-digit integer) as the key."

I don't remember him going over stuff like this so I am kinda lost. Help is needed! Thanks in advance guys.

HashTable <Integer> hashtable = new HashTable <Integer>(1500);



I don't even know how to define a hash function. I know what a hash function is but not how to define one. Help please!

This post has been edited by mattlyons: 07 April 2010 - 08:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HashTable

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: HashTable

Posted 07 April 2010 - 08:15 PM

Don't forget that HashTables accept two generic types- Key and Value. So your HashTable declaration should look more like HashTable<K,V>, where you replace K and V with reference data types.

As for your hash function question, you are basically being asked to write an algorithm to map Keys to Integers, usually consecutive indices like in an array. The Wikipedia Page has a good graphic depicting this if you want to check it out.
Was This Post Helpful? 0
  • +
  • -

#3 mattlyons   User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: HashTable

Posted 07 April 2010 - 08:22 PM

Okay that makes sense. So lets say I do the last 3 digits of the SSN (since first three are similar to people in same areas) will be the Key. How would I actually write that down? Or do I just write it out in words like I just did?
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: HashTable

Posted 07 April 2010 - 09:17 PM

And the class name is Hashtable not HashTable
Was This Post Helpful? 0
  • +
  • -

#5 mattlyons   User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: HashTable

Posted 07 April 2010 - 09:20 PM

Oh okay thanks. Simple mistake there.

Still have the above question if anyone can help.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: HashTable

Posted 07 April 2010 - 09:30 PM

View Postmattlyons, on 07 April 2010 - 09:22 PM, said:

Okay that makes sense. So lets say I do the last 3 digits of the SSN (since first three are similar to people in same areas) will be the Key. How would I actually write that down? Or do I just write it out in words like I just did?

If you used the SSN as an int than

Hastable<Integer, Employe> hash = new Hashtable<Integer, Employe>(1000);

assumming you have a class Employe to store your employes' data
Was This Post Helpful? 0
  • +
  • -

#7 mattlyons   User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: HashTable

Posted 07 April 2010 - 09:41 PM

That means that line of code pbl just did would define the Hashtable. Now how would I actually "define" the hash function? I know the hash function would be the last 3 digits of the SSN. Do I "define" the hash function in code or simply as: "The last 3 digits of each SSN."

If it came up on the test tomorrow to tell what the hash function is, how would I mark it on the test. That is where I am having the problem.
Was This Post Helpful? 0
  • +
  • -

#8 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: HashTable

Posted 07 April 2010 - 09:47 PM

View Postmattlyons, on 07 April 2010 - 10:41 PM, said:

That means that line of code pbl just did would define the Hashtable. Now how would I actually "define" the hash function? I know the hash function would be the last 3 digits of the SSN. Do I "define" the hash function in code or simply as: "The last 3 digits of each SSN."

If it came up on the test tomorrow to tell what the hash function is, how would I mark it on the test. That is where I am having the problem.

You let the Java Hashtable class do it for you
If you want to rewrite your own this is a completly different other story
and then you can actually name your own class HashTable with a capital T and look at macosxnerd101 links if you want to do the hashing yourself
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: HashTable

Posted 07 April 2010 - 10:09 PM

What is the scope of your course? What exactly did you cover in regards to hashing in class? If you covered specific hash algorithms, I would use those. However, if you only covered the basic principle of hashing, I would store the keys into an array and sort them, so that you have an easy perfect hash function as you can use Arrays.sort() or Collections.sort() to do the sorting for you.
Was This Post Helpful? 0
  • +
  • -

#10 mattlyons   User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: HashTable

Posted 07 April 2010 - 10:55 PM

I think I might have figured it out after reading both of your posts. Would this be a hash function:

Hash(key) = 1000 % key

?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: HashTable

Posted 07 April 2010 - 10:59 PM

That would be a hash function, though it would not create a perfect hash, as the keys 996 and 6 would return the same results- 4.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1