I’ve got two ArrayList of String and i want to count words that are in both of them.
Each ArrayList contains keyphrases, for example strings of few words like:
un’altra recessione
strada lunga
presidente barack obama
[…]
I tried with this two methods but i get a NullPointerException
public boolean containsKeyword(String testKeyword, ArrayList<String> list)
{ return list.contains(testKeyword);
}
public int compareKeywords(Quotidiano quotidiano){
int sameKeywords=0;
for(int i=0; i<this.Keywords.size(); i++){
if(this.containsKeyword(this.getKeyword(i), quotidiano.Keywords))
sameKeywords++;
}
return sameKeywords;
}
Thanks a lot for answers!
[EDIT]
It worked using intersection, thank you!
But it would be better if it compares single words.
For example, if i have keyphrases
“spread of italy”,
“spread under 300”,
I think intersection is null, instead i would like “spread” returned.
How can I do it?
Use Apache ListUtils Intersection
PS: This throws
java.lang.NullPointerException– if either list is null