# Brute force searching

I'm currently working on a natural language processing project and I have two ArrayLists<String> where each ArrayList contains a verb at index 0, a noun at index 1 and a noun at index 2 (repeat). I'm trying to find any two nouns in the first list that is present consecutively in the second by using:

 for(int i = 1; i<finalKnowledgeGraph.size(); i+=3) {
for(int j = 1; j<finalKnowledgeGraph.size(); j+=3) {
for(int k=1; k<storeAsserions.size(); k+=3) {
if(finalKnowledgeGraph.get(i).equals(storeAsserions.get(k)) && finalKnowledgeGraph.get(j+1).equals(storeAsserions.get(k+1))){
System.out.println("Found one");
} else if(finalKnowledgeGraph.get(i+1).equals(storeAsserions.get(k)) && finalKnowledgeGraph.get(j).equals(storeAsserions.get(k+1))) {
System.out.println("Found another");
}
}
}
}


However, this code has cubic complexity and both ArrayLists are thousands of lines long. I'm wondering if anyone has any tips on how I could go about speeding up this process. Also, I know virtually nothing about optimization so if you do have some help please break it down relatively simply. One of my friends recently suggested a HashMap and searching through that but in my head that's simply pushing the search problem from one data-structure to another

BTW, your identifier storeAsserions seems to be a typo for storeAssertions. And please include a space after keywords like if & for.