当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java CopyOnWriteArrayList containsAll()用法及代码示例


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

用法

boolean containsAll(Collection col)

参数:此方法接受具有收集类型的强制性参数col。这是一个集合,如果列表中不存在,则需要检查其元素。


返回值:如果列表中存在集合col中的所有元素,则该方法返回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<String> ArrLis1 
            = new CopyOnWriteArrayList<String>(); 
  
        // 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<String> ArrLis2 
            = new CopyOnWriteArrayList<String>(); 
  
        // 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<String> ArrLis3 
            = new CopyOnWriteArrayList<String>(); 
  
        // 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

程序2

// 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<Integer> ArrLis1 
            = new CopyOnWriteArrayList<Integer>(); 
  
        // 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<Integer> ArrLis2 
            = new CopyOnWriteArrayList<Integer>(); 
  
        // 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<Integer> ArrLis3 
            = new CopyOnWriteArrayList<Integer>(); 
  
        // 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://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#containsAll(java.util.Collection)



相关用法


注:本文由纯净天空筛选整理自kanakasrijaathukuri大神的英文原创作品 CopyOnWriteArrayList containsAll() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。