當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。