List<String> actualList = Arrays.asList ("mother has chocolate", "father has dog");
List<String> expectedList = Arrays.asList ("mother", "father", "son", "daughter");
有没有一种方法可以检查expectedList
是否包含actualList
中字符串的任何子字符串?
我找到了一个嵌套的for-each解决方案:
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;
}
我试图寻找一个lambda解决方案,但是我没有找到。我发现所有的方法都是检查String#equals
而不是String#contains
。
如果有类似以下的内容就好了:
CollectionsUtils.containsAny(actualList, exptectedList);
但它使用的是
String#equals
而不是String#contains
来比较字符串。编辑: 根据问题:如果实际列表中的所有子字符串都是期望列表的一部分,则我想要获得TRUE。 并且以下凯文的解决方案适合我。
expectedList
中的每个字符串找到一个匹配; C)actualList
中的每个字符串必须包含在expectedList
中的任意一个字符串中; D) B 和 C 共同满足; E)??? - user85421