• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashMaps

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you supposed to map "crash" to "crashes" "crashed", etc?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you would do well to give us more details of what you have been told to do.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic