Java AbstractCollection containsAll()方法及示例
Java AbstractCollection的containsAll()方法用于检查两个集合是否包含相同的元素。它把一个集合作为参数,如果这个集合的所有元素都存在于另一个集合中,则返回True。
语法
AbstractCollection.containsAll(Collection C)
Java
参数: 参数C是一个集合。这个参数指的是需要在这个集合中检查其元素出现的集合。
返回值: 如果这个集合包含了其他集合的所有元素,该方法返回True,否则返回False。
下面的程序说明了AbstractCollection.conatinsAll()方法。
程序1 :
// Java code to illustrate boolean containsAll()
import java.util.*;
class AbstractCollectionDemo {
public static void main(String args[])
{
// Creating an empty Collection
AbstractCollection<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");
// Creating another empty Collection
AbstractCollection<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");
// Check if the collection
// contains same elements
System.out.println("\nBoth the collections same: "
+ abs.containsAll(abs2));
}
}
Java
输出。
Both the collections same: true
Java
程序 2:
// Java code to illustrate boolean containsAll()
import java.util.*;
class AbstractCollectionDemo {
public static void main(String args[])
{
// Creating an empty Collection
AbstractCollection<String>
abs = new LinkedList<String>();
// Use add() method to
// add elements in the collection
abs.add("Geeks");
abs.add("for");
abs.add("Geeks");
// Creating another empty Collection
AbstractCollection<String>
abs2 = new LinkedList<String>();
// Use add() method to
// add elements in the collection
abs2.add("10");
abs2.add("20");
// Check if the collection
// contains same elements
System.out.println("\nBoth the collections same: "
+ abs.containsAll(abs2));
}
}
Java
输出。
Both the collections same: false
Java