I would probably save the words in the input to the HashSet, and then iterate over the array and see if each one is in the array. contained in a set.
Here it is in the code ... entrance " Around the world in 80 days ."
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { public static void main(final String[] argv) throws FileNotFoundException { final File file; final String[] wordsToFind; file = new File(argv[0]); wordsToFind = getWordsToFind(file); a(file, wordsToFind); b(file, wordsToFind); c(file, wordsToFind); d(file, wordsToFind); }
(after several runs, each run is almost the same):
12596 a: 2440699000 12596 b: 2531635000 12596 c: 4507000 12596 d: 5597000
If you change it by adding "XXX" to each of the words in getWordsToFind (so no words found):
0 a: 7415291000 0 b: 4688973000 0 c: 2849000 0 d: 7981000
And, for completeness, I tried just to find the word "I", and the results:
1 a: 235000 1 b: 351000 1 c: 75000 1 d: 10725000
Tofubeer
source share