Java中的AbstractCollection containsAll() 方法及其示例

Java中的AbstractCollection containsAll() 方法及其示例

Java中AbstractCollection的containsAll()方法用于检查两个集合是否包含相同的元素。它采用一个集合作为参数,如果该集合的所有元素都存在于另一个集合中,则返回True。

语法:

AbstractCollection.containsAll(Collection C)

参数: C是一个集合,这个参数指的是需要在该集合中检查元素出现情况的集合。

返回值: 如果该集合包含另一个集合的所有元素,则方法返回True,否则返回False。

以下程序说明了AbstractCollection.containsAll()方法:

程序1:

// Java code to illustrate boolean containsAll()
  
import java.util.*;
  
class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // 创建一个空集合
        AbstractCollection<String>
            abs = new LinkedList<String>();
  
        // 使用 add() 方法添加元素到集合中
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
        abs.add("10");
        abs.add("20");
  
        // 再创建一个空集合
        AbstractCollection<String>
            abs2 = new LinkedList<String>();
  
        // 使用 add() 方法添加元素到集合中
        abs2.add("Geeks");
        abs2.add("for");
        abs2.add("Geeks");
        abs2.add("10");
        abs2.add("20");
  
        // 检查两个集合是否包含相同的元素
        System.out.println("\nBoth the collections same: "
                           + abs.containsAll(abs2));
    }
}
Both the collections same: true

程序2:

// Java code to illustrate boolean containsAll()
  
import java.util.*;
  
class AbstractCollectionDemo {
    public static void main(String args[])
    {
  
        // 创建一个空集合
        AbstractCollection<String>
            abs = new LinkedList<String>();
  
        // 使用 add() 方法添加元素到集合中
        abs.add("Geeks");
        abs.add("for");
        abs.add("Geeks");
  
        // 再创建一个空集合
        AbstractCollection<String>
            abs2 = new LinkedList<String>();
  
        // 使用 add() 方法添加元素到集合中
        abs2.add("10");
        abs2.add("20");
  
        // 检查两个集合是否包含相同的元素
        System.out.println("\nBoth the collections same: "
                           + abs.containsAll(abs2));
    }
}
Both the collections same: false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程