Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMaps

 
Eli Andrus
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I was wondering, I have to write a code for a HashMap that maps two words to essentially one meaning. For example, I have to make sure that if someone types crash, it will generate the same response as crashes, how would I map these two together?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don’t start by assuming a HashMap is the answer. It might not be. Write down with pencil and paper (and eraser) exactly how you think you ought to solve that problem. That will enable you to go from concept to code.
 
Eli Smies
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well actually, I have to use hash map, this is an assignment, how would I even start doing this though?
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the point of the assignment is to use hashing.

Given that as the case:
If crash hashes to the same value as crashes, use that (the hash) as your key and the meaning as your value. Hash crash and crashes from one thing that you know they share: the meaning of the word.

A little bit of circular reasoning but if the only thing this accomplishes is to get you thinking that's at least a step in the right direction.

And while you're overriding hashCode(), you should probably override equals() as well...just as a complete side note.
 
Rakesh shankar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though if it is through hashing, how would the hash code algorithm would produce the same hash value for both crash and crashes?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you supposed to map "crash" to "crashes" "crashed", etc?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you would do well to give us more details of what you have been told to do.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Campbell. Until you know how you define when two words are equivalent, it's pointless trying to worry about what data structures to use. What you've described so far is potentially a massive task (and probably impossible without a complete dictionary). If it's an assignment I expect something a bit simpler is intended.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic