I have a list of words with a corresponding score for each word. And I'm combing through and matching each individual word in a big block of text in a .txt file with words in the list of words. The .txt file can have up to 10,000 lines of text.
When I first did this, I used a very brute force and naive method to match the words on my word list with my .txt file. Although I used a hash map, I did not use the hash map correctly, and might as well have used it as a list. So the code was written in the following manner:
for(int i=0; i<words.length; i++){
for(int j=0; j<wordListType.size(); j++){
Map<String, Integer> hmap = wordListType.get(j).getMap();
for(Map.Entry<String, Integer> entry : hmap.entrySet()){
if(words[i].contains(entry.getKey())){
foo();
}
}
}
}
words is a String[] with individual words from the text file. wordListType is an ArrayList of a class that contains a hash map of the keywords that I'm searching for. It's an ArrayList because there are multiple types of word lists. And the getMap() is my own helper method inside the WordList class.
Afterwards, I figured out my code was inefficient, and I was not using my hash map to its full strength. So I changed the code to the following:
for(int i=0; i<words.length; i++){
for(int j=0; j<wordListType.size(); j++){
Map<String, Integer> hmap = wordListType.get(j).getMap();
Integer val = null;
if((val = hmap.get(words[i])) != null){
foo();
}
}
}
This way I'm not going through each and every key in hmap like I do with the first method, and I use the O(1) HashMap.get() method instead.
However, the second, efficient method is not producing the results that I want.
I'm not quite sure why the words are being matched differently though. From what I can see, they should both provide the exact same answers, except my latter code should do so much faster. Instead the first method of iterating through all the keys of the hash map is actually producing the results that I want (and I checked this manually), while the second method does not.
There are NO null values in my hash map, which I have tested for. I've looked up the implementation of hash map, so I don't quite understand why this is not working. Am I missing something here or is there something else that is unrelated that is affecting my results? Any help is much appreciated.
k
( inhmap.get(words[k]
) ? shouldn't it bei
? is it a typo? – collapsar Feb 27 '14 at 9:15