File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMaps" Watch "HashMaps" New topic
Author

HashMaps

Eli Andrus
Greenhorn

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
Sheriff

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

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
    
    5

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
Greenhorn

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
Sheriff

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

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

Joined: Apr 06, 2010
Posts: 4366
    
    8

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