List<String> actualList = Arrays.asList ("mother has chocolate", "father has dog");
List<String> expectedList = Arrays.asList ("mother", "father", "son", "daughter");
Is there a way to check if expectedList
contains any substrings of the strings in actualList
?
I found a nested for-each solution:
public static boolean hasAny(List<String> actualList, List<String> expectedList) {
for (String expected: expectedList)
for (String actual: actualList)
if (actual.contains(expected))
return true;
return false;
}
I was trying to a find lambda solution, but I could not. All the methods I found check for String#equals
and not for String#contains
.
It would be nice to have something like:
CollectionsUtils.containsAny(actualList, exptectedList);
But it compares strings using String#equals
not String#contains
.
EDIT:
Based on questions: I want to get TRUE if ALL subStrings from actualList are part of expectedList. And solution from Kevin below works for me.