As the title says, I have an arraylist of over 10000 words and I wish to use another arraylist of words where I chose the words to search for. Cant explain it more simple than that but there's the code you may catch on to what I am trying to achieve. Basically, one arraylist of a lot of words then another arraylist of 5 or so words that will check if those words appear in the long arraylist of words.
//TO DO: Profile the search method
try {
Scanner input = new Scanner(new File("textFile.txt"));
int reps = 100;
List<String> list = new ArrayList();
List<String> searchValues = new ArrayList();
searchValues.add("You");
searchValues.add("and");
searchValues.add("So");
searchValues.add("we");
searchValues.add("important");
while (input.hasNext()) {
list.add(input.next());
}
input.close();
System.out.println("Amount of words in a .txt file: " + list.size());
//Start to time the method
long start = System.currentTimeMillis();
for (int i = 0; i < reps; i++) {
for (int j = 0; j < list.size(); j++) {
//List value = index.search(list.get(j));
List value = index.search(list.get(j));
}
}
long end = System.currentTimeMillis();
System.out.println("Time Taken: " + (end - start) + "ms");
} catch (IOException exc) {
System.out.println("File does not exist");
exc.printStackTrace();
System.exit(1);
}
-
Will take a look now. Thank you.Aleksandar Zoric– Aleksandar Zoric2015年11月13日 14:00:59 +00:00Commented Nov 13, 2015 at 14:00
3 Answers 3
list.retainAll(searchValues)
will remove all of the values from list
which are not in searchValues
. (Javadoc)
Comments
retainAll internally calls contains() which will iterate over the second list, so you get O(n x m) time complexity searching.
You could search for words by using a hashmap, inserting all entries and finding the ones which occured more than once. This should give you 2 x O(n) + O(m).
However, that would likely not matter if your second list only has 5 objects.
Comments
You can also use :
list.stream().filter(s -> ! searchValues.contains(s));
This will return a stream consisting of the elements that don't exist in the searchValues
list (considering that you are working with Java 8).