如何检查字符串 ArrayList 是否包含另一个字符串 ArrayList 的子字符串?

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#equalsnot比较字符串String#contains。


编辑:


基于问题:如果来自actualList 的所有子字符串都是expectedList 的一部分,我想得到TRUE。下面凯文的解决方案对我有用。


慕容森
浏览 262回答 3
3回答

12345678_0001

这样的事情怎么样:list1.stream().allMatch(s1 -> list2.stream().anyMatch(s2 -> s1.contains(s2)))在线试一下。allMatch 将检查是否一切正常 trueanyMatch 将检查是否至少有一个 true这里有一些类似于 Java 7 风格的东西,没有 lambdas 和流,可以更好地理解正在发生的事情:boolean allMatch = true;&nbsp; &nbsp; &nbsp; &nbsp;// Start allMatch at truefor(String s1 : list1){&nbsp; boolean anyMatch = false;&nbsp; &nbsp; // Start anyMatch at false inside the loop&nbsp; for(String s2 : list2){&nbsp; &nbsp; anyMatch = s1.contains(s2);// If any contains is true, anyMatch becomes true as well&nbsp; &nbsp; if(anyMatch)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// And stop the inner loop as soon as we've found a match&nbsp; &nbsp; &nbsp; break;&nbsp; }&nbsp; allMatch = anyMatch;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// If any anyMatch is false, allMatch becomes false as well&nbsp; if(!allMatch)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // And stop the outer loop as soon as we've found a mismatch&nbsp; &nbsp; break;}return allMatch;在线试一下。如果您更喜欢CollectionsUtils.containsAny(list1, list2)可以在代码中的其他地方重用,您可以自己创建一个:public final class CollectionsUtil{&nbsp; public static boolean containsAny(ArrayList<String> list1, ArrayList<String> list2){&nbsp; &nbsp; return list1.stream().allMatch(s1 -> list2.stream().anyMatch(s2 -> s1.contains(s2)));&nbsp; &nbsp; // Or the contents of the Java 7 check-method above if you prefer it&nbsp; }&nbsp; private CollectionsUtil(){&nbsp; &nbsp; // Util class, so it's not initializable&nbsp; }}然后可以根据需要使用:boolean result = CollectionsUtils.containsAny(actualList, expectedList);在线试一下。

米琪卡哇伊

我 99% 确定您不是在hasAny这里寻找最受好评的答案,而是您想查看所有 fromexpectedList是否包含在actualList. 为此,首先创建 aSet和它的工作将是有益的(因为contains是O(1)forHashSet和反对O(n)for List)。现在想一想,因为您只需要contains,您可以将其拆分actualList并从中创建独特的单词:private static boolean test(List<String> actualList, List<String> expectedList) {&nbsp; &nbsp; Pattern p = Pattern.compile("\\s+");&nbsp; &nbsp; Set<String> set = actualList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(p::splitAsStream)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toSet());&nbsp; &nbsp; return expectedList.stream().allMatch(set::contains);}

守候你守候我

public static boolean containsAny(List<String> actualList, List<String> expectedList) {&nbsp; &nbsp; final Pattern words = Pattern.compile("\\s+");&nbsp; &nbsp; return actualList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.flatMap(words::splitAsStream)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.distinct()//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.allMatch(expectedList::contains)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.anyMatch(expectedList::contains);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java