Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NaturalLanguageMultiply- Hash map

 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I'm not sure how to use this HashMap object. Do I need to create arrays like for Say.java to put these values into a Hash map object? Or do I just reuse a method in the HashMap object 28 times to place these values in? Or is there some other way? Pleas e can you help as I am not familiar with the HashMap object.
Thanks
Yen
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yen,
Hava a look at this thread Making Hash!
Good luck
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Please I need some more help. I'm thinking of creating an array like in Say to contain the words for the numerics and then using a for loop to cycle this to hold these details in a hashmap.However, it seems like replication as we will then hold the values in an array and a hashmap.
Another way would be to manually repeat a method from the class hashmap 28 times to initialise the hashmap, but this also seems longwinded
Pleeeeeeeeeeeeeaaaaaaaaaaaaaaaassssssssssse help
Yen
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may seem longwinded but it works. The way you are talking about, with the array, uses more resources than just building the HashMap yourself. Don't know if you would get nitpicked for doing it that way, though. Do whatever is the most readable
Jason
 
Richard Boren
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yen Cheong,
You do not need an array nor do you need to create any logic to load the Hashmap. Simple load the Hashmap directly using its put() method. Also study the example for the assignment covering a static Vector. Instead of a static Vector you will use a static Hashmap. Your code for loading the Hashmap should look very similiar to the Vector example, except you will use a Hashmap which stores key/value pairs. The key is then used to retrieve the value from the Hashmap using its get() method.
Hope this helps
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jason adam:
Don't know if you would get nitpicked for doing it that way, though. Do whatever is the most readable

Yup, this gets nitpicked.
Using a loop to load the HashMap seemed so clever (and one way of not repeating that method 28 times), but after looking at how clear the code is when you just "put" in the keys and values directly, there's no question about which option is more readable.
Live and learn.
Pauline
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic