I\'m building a thesaurus using a HashMap to store the synonyms.
I\'m trying to search through the words based on a regular expression: the method will have to take
Responding to Jay of "But Hmm" above,
(I'd add a comment but don't have the rep.)
Searching it sequentially is doing it the slow way. Doing it with regular expressions is to descend into madness. Doing it with a database is a programming cop out. Sure if your data set was massive that might be required but remember "for this assignment we're asked to use Java Collection Map" We should be figuring out the proper way to use this java collection.
The reason it isn't obvious is because it isn't one collection. It's two. But it isn't two maps. It’s not an ArrayList. What’s missing is a Set. It's a map to sets of synonyms.
Set
Map
Build your sets. Then build the map. Write a helper method to build the map that takes a map and a set.
addSet(Map
This method just loops newSet and adds the strings to the map as keys and the reference to newSet as the value. You’d call addSet once for every set.
Now that you're data structure is built we should be able to find stuff. To make that a little more robust, remember to clean your search key before you search. Use trim() to get rid of meaningless whitespace. Use toLowerCase() to get rid of meaningless capitalization. You should have done both of these on the synonym data before (or while) building the sets. Do that and who needs regular expressions for this? This way is much faster and more importantly safer. Regular Expressions are very powerful but can be a nightmare to debug when they go wrong. Don't use them just because you think they're cool.