List<String> actualList = Arrays.asList ("mother has chocolate", "father has dog");
List<String> expectedList = Arrays.asList ("mother", "father", "son", "daughter");
Run Code Online (Sandbox Code Playgroud)
有没有办法检查是否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;
}
Run Code Online (Sandbox Code Playgroud)
我试图寻找lambda解决方案,但我不能.我找到的所有方法都检查String#equals而不是String#contains.
有这样的东西会很高兴:
CollectionsUtils.containsAny(actualList, exptectedList);
Run Code Online (Sandbox Code Playgroud)
但它使用String#equalsnot 来比较字符串String#contains.
编辑:
基于问题:如果来自actualList的所有subStrings都是expectedList的一部分,我想得到TRUE.以下凯文的解决方案适合我.