jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes HashMaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMaps" Watch "HashMaps" New topic


Eli Andrus

Joined: Oct 17, 2011
Posts: 20
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

Joined: Oct 13, 2005
Posts: 37900
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

Joined: Oct 17, 2011
Posts: 2
well actually, I have to use hash map, this is an assignment, how would I even start doing this though?
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171

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.

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Rakesh shankar

Joined: Nov 12, 2010
Posts: 14
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

Joined: Oct 13, 2005
Posts: 37900
Are you supposed to map "crash" to "crashes" "crashed", etc?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37900
I think you would do well to give us more details of what you have been told to do.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4338

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.
I agree. Here's the link: http://aspose.com/file-tools
subject: HashMaps
Similar Threads
JTable - Renderer for JPanel
certificate and card
What is your favorite letter?
questions about Process and Runtime()
Do you know what your keys do?