The program I'm working on now has one part that takes a little time. Basically, I have a list of lines and one target phrase. For example, suppose the target phrase is “finished goods inventory”. Now, after filtering the word stop (of), I want to extract all the lines from a list containing one of three words: "inventory", "finished" and "product". Now I implemented this idea as follows:
String[] targetWords; // contains "inventory", "finished", and "goods" ArrayList<String> extractedStrings = new ArrayList<String>(); for (int i = 0; i < listOfWords.size(); i++) { String[] words = listOfWords.get(i).split(" "); outerloop: for (int j = 0; j < words.length; j++) { for (int k = 0; k < targetWords.length; k++) { if (words[j].equalsIgnoreCase(targetWords[k])) { extractedStrings.add(listOfWords.get(i)); break outerloop; } } } }
The list contains more than 100 thousand words, and it takes from 4 to 0.8 seconds to complete the task for each target phrase. The fact is, I have a lot of these target phrases to process, and the seconds really add up. So I was wondering if anyone knows a more efficient way to accomplish this task? Thanks for the help in advance!
source share