Hi I am populating a Hashmap with a dictionary.txt file and I am splitting the hashmap into sets of word lengths.
Im having trouble searching the Hashmap for a pattern of “a*d**k”;
Can anyone help me?
I need to know how to search a Hashmap?
I would really appreciate if you could help me.
Thank you.
A
HashMapis simply the wrong data structure for a pattern search.You should look into technologies that feature pattern searching out of the box, like Lucene
And in answer to this comment:
HashMapsare awfully fast, that’s true, but only if you use them as intended. In your scenario, hash codes are not important, as you know that all keys are numeric and you probably won’t have any word that’s longer than, say, 30 letters.So why not just use an Array or ArrayList of Sets instead of a HashMap and replace
map.get(string.length())withlist.get(string.length()-1)orarray[string.length()-1]. I bet the performance will be better than with a HashMap (but we won’t be able to tell the difference unless you have a reaaaallly old machine or gazillions of entries).I’m not saying my design with a List or Array is nicer, but you are using a data structure for a purpose it wasn’t intended for.
Seriously: How about writing all your words to a flat file (one word per line, sorted by word length and then by alphabetically) and just running the regex query on that file? Stream the file and search the individual lines if it’s too large, or read it as a String and keep that in memory if IO is too slow.
Or how about just using a
TreeSetwith a customComparator?Sample code: