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


Java CopyOnWriteArraySet containsAll()用法及代碼示例


Java CopyOnWriteArraySet的containsAll()方法用於檢查兩個集合是否包含相同的元素。它使用一個集合作為參數,如果該集合的所有元素都存在於另一個集合中,則返回True。

用法:

public boolean containsAll(Collection C)

參數:參數C是一個Collection。此參數表示需要在此集合中檢查其元素出現的集合。


返回值:如果此集合包含其他集合的所有元素,則該方法返回True,否則返回False。

以下示例程序旨在說明CopyOnWriteArraySet.containsAll()方法:

示例1:

// Java code to illustrate 
// CopyOnWriteArraySet containsAll() 
  
import java.util.*; 
import java.util.concurrent.*; 
  
class StackDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty set 
        CopyOnWriteArraySet<String> 
            set = new CopyOnWriteArraySet<String>(); 
  
        // Use add() method to 
        // add elements in the set 
        set.add("Geeks"); 
        set.add("for"); 
        set.add("Geeks"); 
        set.add("10"); 
        set.add("20"); 
  
        // prints the set 
        System.out.println("CopyOnWriteArraySet 1: "
                           + set); 
  
        // Creating another empty set 
        CopyOnWriteArraySet<String> 
            set2 = new CopyOnWriteArraySet<String>(); 
  
        // Use add() method to 
        // add elements in the set 
        set2.add("Geeks"); 
        set2.add("for"); 
        set2.add("Geeks"); 
        set2.add("10"); 
        set2.add("20"); 
  
        // prints the set 
        System.out.println("CopyOnWriteArraySet 2: "
                           + set2); 
  
        // Check if the set 
        // contains same elements 
        System.out.println("\nDoes set 1 contains set 2: "
                           + set.containsAll(set2)); 
    } 
}
輸出:
CopyOnWriteArraySet 1: [Geeks, for, 10, 20]
CopyOnWriteArraySet 2: [Geeks, for, 10, 20]

Does set 1 contains set 2: true
輸出:
CopyOnWriteArraySet 1: [Geeks, for, Geeks, 10, 20]
CopyOnWriteArraySet 2: [Geeks, for, Geeks, 10, 20]

Does set 1 contains set 2: true

示例2:

// Java code to illustrate boolean containsAll() 
  
import java.util.*; 
import java.util.concurrent.*; 
  
class StackDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty set 
        CopyOnWriteArraySet<String> 
            set = new CopyOnWriteArraySet<String>(); 
  
        // Use add() method to 
        // add elements in the set 
        set.add("Geeks"); 
        set.add("for"); 
        set.add("Geeks"); 
  
        // prints the set 
        System.out.println("CopyOnWriteArraySet 1: "
                           + set); 
  
        // Creating another empty set 
        CopyOnWriteArraySet<String> 
            set2 = new CopyOnWriteArraySet<String>(); 
  
        // Use add() method to 
        // add elements in the set 
        set2.add("10"); 
        set2.add("20"); 
  
        // prints the set 
        System.out.println("CopyOnWriteArraySet 2: "
                           + set2); 
  
        // Check if the set 
        // contains same elements 
        System.out.println("\nDoes set 1 contains set 2: "
                           + set.containsAll(set2)); 
    } 
}
輸出:
CopyOnWriteArraySet 1: [Geeks, for]
CopyOnWriteArraySet 2: [10, 20]

Does set 1 contains set 2: false


相關用法


注:本文由純淨天空篩選整理自akshajjuneja9大神的英文原創作品 CopyOnWriteArraySet containsAll() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。