• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

building a simple hash table from scratch

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, I am totally new here and also 1 year in learning java.
these days I am trying to understand how hash Tables works in java. I am trying to build a simple hash table from scratch,
that table would hold key and value both of type string. I am not allowed to use any generic classes here in my code.
I wan't to define tow methods for this table , set() and get()
can someone please tell me why my program dos not print out the Value "kassoum" for the key "sammy", instead I got null! ?
Thanks in advance
 
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I have moved your topic to "Java in General" as it is too difficult for "Beginning Java".
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you trying to debug the code to see what it is doing?  I use print statements that print out the values of variables as their values are changed and as they are used.  The print out will show you what the code is doing and help you find the problem.

Note: A helpful technique that goes with the above is to define a toString() method in the Node class so when a Node is printed, its contents will be formatted.
Another useful debugging tool is the Arrays class's toString method for formatting arrays for printing:
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:Welcome to the Ranch.

I have moved your topic to "Java in General" as it is too difficult for "Beginning Java".


Thanks Vilda, I see, I agree with you it is a difficult one
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With normal debugging techniques it should be easy to find the problem and fix it.

What have you tried?
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:How are you trying to debug the code to see what it is doing?  I use print statements that print out the values of variables as their values are changed and as they are used.  The print out will show you what the code is doing and help you find the problem.

Note: A helpful technique that goes with the above is to define a toString() method in the Node class so when a Node is printed, its contents will be formatted.
Another useful debugging tool is the Arrays class's toString method for formatting arrays for printing:



Thanks Norm Radder
for debugging usually I just go through the code and read it thoroughly, sometimes I try to eliminate portions of the code and try to run the other.
thanks for your advise to use the toString() method. I am working now on the code and I discovered mistakes in inserting new item to a linked list.
maybe my question is too big to be discussed here...
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:With normal debugging techniques it should be easy to find the problem and fix it.

What have you tried?


Hi Norm !

this is what I have tired so far
now it seems that I have a problem with the get()
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

question is too big


No, the question is about a normal type of problem that students have.  It might be past a beginner's problem but it's not too big for here.

I have a problem with the get()  


Please explain.  Copy and paste any error messages.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when I run the code I receive the following :-
run:
location value in put is 1
location value for get is 1
Exception in thread "main" java.lang.NullPointerException
this is the value :- 0547723878
at javaapplication1.SimpleHashTable.get(SimpleHashTable.java:62)
at javaapplication1.SimpleHashTable.test(SimpleHashTable.java:47)
at javaapplication1.SimpleHashTable.main(SimpleHashTable.java:11)
C:\Users\Sami Kassoum\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

so far the put() was executed and a key + value where added, inside the get() going inside the while loop I managed to print out the value of the key "sammy", but the function fails to return the value of type string.
I have a nullPointer Error, it seems that I have gone with my 'runner' variable too far ....
I am trying to figure out why ?..
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it seems that I have gone with my 'runner' variable too far ....  


can you explain what this code from the get() method is supposed to do?

Note the else clause is missing {}s to enclose the code it controls.  You should always use {}s with if, else and loops
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:
can you explain what this code from the get() method is supposed to do?
Note the else clause is missing {}s to enclose the code it controls.  You should always use {}s with if, else and loops


i want the get() method to print me all poissible values for my specefic key.
so I realized now that I should print the values as soon as I read them and not to return them from the method.
yes you are sure for the {} of the else , I didn't noticed that,
so here what I changed in my code and now it is running without any errors, I also fixed the null pointer error

 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 want the get() method to print me all poissible values for my specefic key.  



That's a new meaning for a get() method's purpose.  Normally a get() method would return the value associated with the key passed as argument.
Your get() method shows all the values in the hash bucket for the key that was passed as an arg.   That may be a valid method for debugging, but it's not useful for someone trying to get the value associated with a key.
 
Liutauras Vilda
Marshal
Posts: 8969
646
Mac OS X Spring VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Norm, have a cow for a helpfulness in the whole thread.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:
That's a new meaning for a get() method's purpose.  Normally a get() method would return the value associated with the key passed as argument.
Your get() method shows all the values in the hash bucket for the key that was passed as an arg.   That may be a valid method for debugging, but it's not useful for someone trying to get the value associated with a key.


I thought that a hash table stores  one key value in each linked list and then associate to that key as many as possible value keys.
like if you have a person name as a key, then you associate multiple phone numbers for that person as values.

what I understand from you is that in one linked list inside the hash table we can find in each node different keys associated with there unique values. so in this case it would be easier to find the value
then the get() method indeed should return us a value...
so I have to change the code again....
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have to change the code  


Yes.  Each bucket in the array has a linked list of the key/value pairs for all the keys with the same hash code.  The code needs to search the list for the key that matches the arg.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:
Yes.  Each bucket in the array has a linked list of the key/value pairs for all the keys with the same hash code.  The code needs to search the list for the key that matches the arg.


great now I understand ! different key values might have the same hash code , so I have to seek inside that bucket that its address was provided after the hash code was  generated.
so I have changed my code and now it looks like this, I also managed to  store two key-value entries
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I assume that the code is working now.  Now its time for some heavier testing. Something that will cause multiple keys to have the same hashcode
Use a loop to add lots of key/value pairs ( 1000 or 10000) with the key and value being the String value of the loop index.
So for example the keys would range from "0" to "99999";  Then do some gets.

A useful check on the efficiency of the hash would be to loop through the hashTable array and print out the length of each linked list.  The idea hash would have all the lists about the same length.

Note: The code should not use hardcoded value 10 for the hash.  It should use the length of the array.  That would allow a different number of chains instead of always 10.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Norm  
Sorry for answering late, I was out joging
.  Your offer sounds very interesting! So the idea is to fill 10000 nodes inside the hash table,  for the keys I will use the loop numbers,  I will wrap them in the Integer object, then I invoke the toString()  method,  for the value I can randomly create numbers then entering them as strings.  For now I have no idea how to generate strings in java, so I will stick to what I mentioned,.... I think I'll have the code tomorrow since it is getting too late here
 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe 10000 is too many for now.  Try something smaller first, like 40.  Print out the contents of the hashTable array and manually inspect that all the added elements are there.
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Maybe 10000 is too many for now.  Try something smaller first, like 40.  Print out the contents of the hashTable array and manually inspect that all the added elements are there.


Hi Norm here I have entered 40 pairs of (key,Value) pairs as you asked, I have also done some improvement for the code :
the program looks like it is working like it should be.
here is the code :-


--------------------------------------------------------------------
the flowing is the output of the program
run:
the folowing are the (key,value) pairs as they are inserted to the hash Table

(0 , 145)
(1 , 362)
(2 , 810)
(3 , 588)
(4 , 993)
(5 , 568)
(6 , 585)
(7 , 671)
(8 , 608)
(9 , 274)
(10 , 391)
(11 , 387)
(12 , 756)
(13 , 871)
(14 , 32)
(15 , 49)
(16 , 370)
(17 , 112)
(18 , 47)
(19 , 281)
(20 , 873)
(21 , 175)
(22 , 723)
(23 , 279)
(24 , 524)
(25 , 413)
(26 , 816)
(27 , 482)
(28 , 461)
(29 , 917)
(30 , 65)
(31 , 855)
(32 , 635)
(33 , 91)
(34 , 653)
(35 , 73)
(36 , 693)
(37 , 971)
(38 , 618)
(39 , 171)
----------------------------------------------------------------

the folowing are the (key,value) pairs as they are printd out from hash Table
(0 , 145)
(1 , 362)
(2 , 810)
(3 , 588)
(4 , 993)
(5 , 568)
(6 , 585)
(7 , 671)
(8 , 608)
(9 , 274)
(10 , 391)
(11 , 387)
(12 , 756)
(13 , 871)
(14 , 32)
(15 , 49)
(16 , 370)
(17 , 112)
(18 , 47)
(19 , 281)
(20 , 873)
(21 , 175)
(22 , 723)
(23 , 279)
(24 , 524)
(25 , 413)
(26 , 816)
(27 , 482)
(28 , 461)
(29 , 917)
(30 , 65)
(31 , 855)
(32 , 635)
(33 , 91)
(34 , 653)
(35 , 73)
(36 , 693)
(37 , 971)
(38 , 618)
(39 , 171)

the folowing are the length of each linked list inside the hash table

number of nodes in the 0th linked list is 4
number of nodes in the 1th linked list is 4
number of nodes in the 2th linked list is 4
number of nodes in the 3th linked list is 4
number of nodes in the 4th linked list is 4
number of nodes in the 5th linked list is 4
number of nodes in the 6th linked list is 4
number of nodes in the 7th linked list is 4
number of nodes in the 8th linked list is 4
number of nodes in the 9th linked list is 4
BUILD SUCCESSFUL (total time: 0 seconds)

 
Norm Radder
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, looks a lot better.
Except:
the code still uses hardcoded 10 vs tableSize on lines 22 and 70

Lines 31 to 41 could be simplified to this:

Another technique I use for debugging Nodes in a list is to add a toString() method to the Node class:
 
Sami Kassoum
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you Norm for your great help , I took your comments into my code and it works fine now.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic