【问题标题】:Check if an ArrayList contains every element from another ArrayList (or Collection)检查 ArrayList 是否包含来自另一个 ArrayList(或集合)的每个元素
【发布时间】:2013-01-08 19:37:28
【问题描述】:

我可能在这里找不到一个简单的单行,但这是我的问题:

如何检查一个 ArrayList 是否包含另一个 ArrayList 中的所有对象?我正在寻找(如果存在的话)以下内容:

//INCORRECT EXAMPLE:
if(one.contains(two))
{
    return true;
}
else
{
    return false;
}

例如:

ArrayList one = {1, 2, 3, 4, 5}

ArrayList two = {1, 2, 3} --> True
ArrayList two = {} --> True
ArrayList two = {1, 2, 3, 4, 5} --> True
ArrayList two = {1, 5, 2} --> True
ArrayList two = {1, 7, 4} --> False
ArrayList two = {0, 1, 3} --> False
ArrayList two = {4, 5, 6} --> False
ArrayList two = {7, 8, 9} --> False

【问题讨论】:

标签: java arraylist comparison contains


【解决方案1】:

java.util.Collection 接口中声明了一个名为containsAll 的方法。在您的设置中,one.containsAll(two) 给出了想要的答案。

【讨论】:

    【解决方案2】:

    根据列表界面:

    myList.containsAll(...);
    

    【讨论】:

      【解决方案3】:

      看看List接口中的containsAll(Collection<?> c)方法。我想这就是你要找的。​​p>

      【讨论】:

        【解决方案4】:

        这是另一个使用 containsAll() 的示例,我用它来断言 JUnit 测试中两个数组相等:

        List<String> expected = new ArrayList<String>();
        expected.add("this");
        expected.add("that");
        expected.add("another");
        
        List<String> actual = new ArrayListString();
        actual.add("another");
        actual.add("that");
        actual.add("this");
        
        Assert.assertTrue("The lists do not match!", expected.containsAll(actual));
        

        【讨论】:

          【解决方案5】:

          您可以使用列表的containsAll 方法进行检查。然而,这是一个线性操作。如果列表很大,应该先转换成HashSet,然后再执行containsAll

          HashSet tmp = new HashSet(one);
          if (tmp.containsAll(two)) {
              ...
          }
          

          如果one的长度为N,二的长度为M,则此解的时间复杂度为O(M+N); “普通”containsAll 具有O(M*N) 的复杂性,可能会更糟。

          【讨论】:

            【解决方案6】:

            您在示例中的代码没有意义,但无论如何这里是一个示例。

            ArrayList<Integer> one, two;
            //initialize
            boolean good = true;
            for (int i = 0; i < two.size(); i ++) {
                if (!(one.contains(two.get(i))) {
                    good = false;
                    break;
                }
            }
            

            它只是循环遍历two 的所有元素并检查它们是否在one 中。

            然后布尔值good 包含您想要的值。

            ArrayList#contains

            编辑:哦,哇,我完全忘记了containsAll。哦,好吧,如果您真的想了解它,这是另一种方法。

            【讨论】:

            • 一旦它变成“假”,你就应该“打破”。无需继续检查其余部分。
            • 或者(而不是 break)你可以这样做: for (int i = 0; i
            • 如果你知道 'one' 是从小到大排序的,你可以在实际代码中进行二进制搜索,但这只会在时间上有所帮助,如果你期望 'one' 相当大。
            【解决方案7】:

            这也可以在 Java 中使用流来完成

                List<String> employeeList = Arrays.asList("Marc","john");
                List<String> masterEmployeeList = Arrays.asList("Marc", "Stacy", "john");
                System.out.println(employeeList.stream().allMatch(masterEmployeeList::contains));
            

            【讨论】:

              【解决方案8】:

              在 org.apache.commons.collections.CollectionUtils 中声明的 isEqualCollection() 方法为您提供集合是否相同。

              if (CollectionUtils.isEqualCollection(collectionA,collectionB)) { 做... }

              【讨论】:

                猜你喜欢
                • 2021-01-03
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-08-26
                • 2012-05-03
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多