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


Java AbstractSequentialList conatinsAll()用法及代碼示例


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

用法:

public boolean containsAll(Collection C)

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


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

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

程序1:

// Java code to illustrate 
// AbstractSequentialList containsAll() 
  
import java.util.*; 
  
class AbstractSequentialListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty Collection 
        AbstractSequentialList<String> 
            abs = new LinkedList<String>(); 
  
        // Use add() method to 
        // add elements in the collection 
        abs.add("Geeks"); 
        abs.add("for"); 
        abs.add("Geeks"); 
        abs.add("10"); 
        abs.add("20"); 
  
        // prints the list 
        System.out.println("AbstractSequentialList 1:"
                           + abs); 
  
        // Creating another empty Collection 
        AbstractSequentialList<String> 
            abs2 = new LinkedList<String>(); 
  
        // Use add() method to 
        // add elements in the collection 
        abs2.add("Geeks"); 
        abs2.add("for"); 
        abs2.add("Geeks"); 
        abs2.add("10"); 
        abs2.add("20"); 
  
        // prints the list 
        System.out.println("AbstractSequentialList 2:"
                           + abs2); 
  
        // Check if the collection 
        // contains same elements 
        System.out.println("\nBoth the collections same:"
                           + abs.containsAll(abs2)); 
    } 
}
輸出:
AbstractSequentialList 1:[Geeks, for, Geeks, 10, 20]
AbstractSequentialList 2:[Geeks, for, Geeks, 10, 20]

Both the collections same:true

程序2:

// Java code to illustrate boolean containsAll() 
  
import java.util.*; 
  
class AbstractSequentialListDemo { 
    public static void main(String args[]) 
    { 
  
        // Creating an empty Collection 
        AbstractSequentialList<String> 
            abs = new LinkedList<String>(); 
  
        // Use add() method to 
        // add elements in the collection 
        abs.add("Geeks"); 
        abs.add("for"); 
        abs.add("Geeks"); 
  
        // prints the list 
        System.out.println("AbstractSequentialList 1:"
                           + abs); 
  
        // Creating another empty Collection 
        AbstractSequentialList<String> 
            abs2 = new LinkedList<String>(); 
  
        // Use add() method to 
        // add elements in the collection 
        abs2.add("10"); 
        abs2.add("20"); 
  
        // prints the list 
        System.out.println("AbstractSequentialList 2:"
                           + abs2); 
  
        // Check if the collection 
        // contains same elements 
        System.out.println("\nAre both collections same:"
                           + abs.containsAll(abs2)); 
    } 
}
輸出:
AbstractSequentialList 1:[Geeks, for, Geeks]
AbstractSequentialList 2:[10, 20]

Are both collections same:false


相關用法


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