📌  相关文章
📜  Java中的 CopyOnWriteArrayList containsAll() 方法

📅  最后修改于: 2022-05-13 01:54:50.669000             🧑  作者: Mango

Java中的 CopyOnWriteArrayList containsAll() 方法

Java中 CopyOnWriteArrayList 类的 containsAll() 方法用于检查此 List 是否包含指定 Collection 中的所有元素。所以基本上它用于检查 List 是否包含一组元素。

语法

boolean containsAll(Collection col)

参数:此方法接受一个强制参数 col,该参数属于集合类型。这是需要检查其元素是否存在于列表中的集合。

返回值:如果集合 col 中的所有元素都存在于 List 中,则该方法返回 True,否则返回 False。

异常:如果指定的集合为 NULL,该方法将抛出 NullPointerException。

下面的程序说明了 CopyOnWriteArrayList 类的 containsAll() 方法:

程序 1

// Java program to illustrate CopyOnWriteArrayList
//  containsAll() method
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] args)
    {
        CopyOnWriteArrayList ArrLis1
            = new CopyOnWriteArrayList();
  
        // Add elements
        ArrLis1.add("geeks");
        ArrLis1.add("shaan");
        ArrLis1.add("gfg");
        ArrLis1.add("programming");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList1: " + ArrLis1);
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList ArrLis2
            = new CopyOnWriteArrayList();
  
        // Add elements
        ArrLis2.add("geeks");
        ArrLis2.add("gfg");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList2: " + ArrLis2);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis2))
            System.out.println("All list2 elements are present in list1");
        else
            System.out.println("All list2 elements are not present in list1");
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList ArrLis3
            = new CopyOnWriteArrayList();
  
        // Add elements
        ArrLis3.add("geeks");
        ArrLis3.add("qwe");
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList3: " + ArrLis3);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis3))
            System.out.println("All list3 elements are present in list1");
        else
            System.out.println("All list3 elements are not present in list1");
    }
}
输出:
CopyOnWriteArrayList1: [geeks, shaan, gfg, programming]
CopyOnWriteArrayList2: [geeks, gfg]
All list2 elements are present in list1
CopyOnWriteArrayList3: [geeks, qwe]
All list3 elements are not present in list1

方案二

// Java program to illustrate CopyOnWriteArrayList
// containsAll() method
  
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] args)
    {
        CopyOnWriteArrayList ArrLis1
            = new CopyOnWriteArrayList();
  
        // Add elements
        ArrLis1.add(23);
        ArrLis1.add(65);
        ArrLis1.add(91);
        ArrLis1.add(126);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList1: " + ArrLis1);
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList ArrLis2
            = new CopyOnWriteArrayList();
  
        // Add elements
        ArrLis2.add(23);
        ArrLis2.add(126);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList2: " + ArrLis2);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis2))
            System.out.println("All list2 elements are present in list1");
        else
            System.out.println("All list2 elements are not present in list1");
  
        // create object of CopyOnWriteArrayList
        CopyOnWriteArrayList ArrLis3
            = new CopyOnWriteArrayList();
  
        // Add elements
        ArrLis3.add(23);
        ArrLis3.add(92);
  
        // print CopyOnWriteArrayList
        System.out.println("CopyOnWriteArrayList3: " + ArrLis3);
  
        // check using function
        if (ArrLis1.containsAll(ArrLis3))
            System.out.println("All list3 elements are present in list1");
        else
            System.out.println("All list3 elements are not present in list1");
    }
}
输出:
CopyOnWriteArrayList1: [23, 65, 91, 126]
CopyOnWriteArrayList2: [23, 126]
All list2 elements are present in list1
CopyOnWriteArrayList3: [23, 92]
All list3 elements are not present in list1

参考:https: Java Java.util.Collection)