Java AbstractCollection类的containsAll()方法
该containsAll()
方法检查指定集合中的所有元素。如果此集合具有所有元素,则返回TRUE。方法逐个检查每个元素,以查看它是否包含在此集合中。
语法如下-
public boolean containsAll(Collection<?> c)
要使用Java中的AbstractCollection类,请导入以下包-
import java.util.AbstractCollection;
以下是containsAll()
在Java中实现AbstractCollection方法的示例-
示例
import java.util.ArrayList; import java.util.AbstractCollection; public class Demo { public static void main(String[] args) { AbstractCollection<Object> absCollection1 = new ArrayList<Object>(); absCollection1.add("These"); absCollection1.add("are"); absCollection1.add("demo"); absCollection1.add("elements"); System.out.println("AbstractCollection1: " + absCollection1); AbstractCollection<Object> absCollection2 = new ArrayList<Object>(); absCollection2.add("These"); absCollection2.add("are"); absCollection2.add("demo"); absCollection2.add("elements"); System.out.println("AbstractCollection2: " + absCollection2); System.out.println("Is both the collection having identical elemenst? " + absCollection1.containsAll(absCollection2)); } }
输出结果
AbstractCollection1: [These, are, demo, elements] AbstractCollection2: [These, are, demo, elements] Is both the collection having identical elemenst? true