I am looking for a better way to check if a string contains a substring from a list of keywords.
For example, I create a list like this:
List<String> keywords = new ArrayList<>();
keywords.add("mary");
keywords.add("lamb");
String s1 = "mary is a good girl";
String s2 = "she likes travelling";
Line s1 has a "mary" of keywords, but line s2 does not. So, I would like to define a method:
boolean containsAKeyword(String str, List<String> keywords)
Where it containsAKeyword(s1, keywords)returns true, but containsAKeyword(s2, keywords)returns false. I can return true even if there is one subscript match.
I know that I can just iterate over the list of keywords and call str.contains () for each item in the list, but I was wondering if there is a better way to iterate over the entire list (avoid the complexity of O (n)), or if Java provides for of this inline methods.