0

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);
 }
asked Nov 13, 2015 at 13:58
1
  • Will take a look now. Thank you. Commented Nov 13, 2015 at 14:00

3 Answers 3

3
list.retainAll(searchValues)

will remove all of the values from list which are not in searchValues. (Javadoc)

answered Nov 13, 2015 at 14:00

Comments

1

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.

answered Nov 13, 2015 at 14:12

Comments

0

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).

answered Nov 13, 2015 at 14:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.