当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Collection containsAll()用法及代码示例


集合Java 中的接口是 Collection 层次结构的基本部分。它被发现于java.util封装并提供了广泛的内置类和方法。一种这样有用的方法是containsAll().

JavacontainsAll()方法

这样做的目的containsAll()是检查一个集合是否包含另一个集合或所提到的集合中存在的所有元素。

containsAll() 的语法

boolean containsAll(Collection<?> obj)

参数:它接受一个 Collection 对象作为参数,该参数将与当前集合进行检查。

返回类型:It 返回boolean (或者`true` 或 `false`)

true: if the collection object contains all the elements as in the current collection.
false: if the collection object elements don’t match with the current collection elements (if there is a single mismatch, it returns false).

当前集合对象和比较集合对象中的元素数量不必相同。

JavacontainsAll()方法示例

Java containsAll() 方法可以用任何集合来实现,其中一些方法在下面的示例中提到。

示例1:(使用列表)

下面是上述方法的实现:

Java


// Java Program demomstrate 
// Java containsAll() method 
import java.io.*; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 
  
// Driver Class 
class GFG { 
    // main function 
    public static void main(String[] args) 
    { 
        // define an ArrayList to store [1,2,3,4,5] 
        List<Integer> list1 = new ArrayList<Integer>( 
            Arrays.asList(1, 2, 3, 4, 5)); 
  
        // define an ArrayList to store [1,2,3] 
        List<Integer> list2 = new ArrayList<Integer>( 
            Arrays.asList(1, 2, 3)); 
  
        // define an ArrayList to store [1,2,7] 
        List<Integer> list3 = new ArrayList<Integer>( 
            Arrays.asList(1, 2, 7)); 
  
        // [1,2,3,4,5] 
        System.out.println("List1 elements: " + list1); 
        // [1,2,3] 
        System.out.println("List2 elements: " + list2); 
  
        // the below step prints "true" because elements 
        // [1,2,3] are present in [1,2,3,4,5] 
        System.out.println( 
            "Is the List1 contains all the elements of List2 ? "
            + list1.containsAll(list2)); 
        //[1,2,7] 
        System.out.println("List3 elements: " + list3); 
  
        // the below step prints "false" because 7 is not 
        // present in [1,2,3,4,5] 
        System.out.println( 
            "Is the List1 contains all the elements of List3 ? "
            + list1.containsAll(list3)); 
    } 
}
输出
List1 elements: [1, 2, 3, 4, 5]
List2 elements: [1, 2, 3]
Is the List1 contains all the elements of List2 ? true
List3 elements: [1, 2, 7]
Is the List1 contains all the elements of List3 ? false

示例2:(使用集合)

下面是上述方法的实现:

Java


// Java Program demomstrate 
// Java containsAll() method 
import java.io.*; 
import java.util.HashSet; 
import java.util.Set; 
  
// Driver Class 
class GFG { 
      // main function 
    public static void main(String[] args) 
    { 
        // define set1 to store the elements 
        // stores ["apple","tomato", "carrot"] 
        Set<String> set1 = new HashSet<String>(); 
        set1.add("carrot"); 
        set1.add("apple"); 
        set1.add("tomato"); 
  
        // define set2 to store the elements 
        // stores ["apple", "banana", "carrot", "tomato"] 
        Set<String> set2 = new HashSet<String>(); 
        set2.add("carrot"); 
        set2.add("apple"); 
        set2.add("tomato"); 
        set2.add("apple"); 
        set2.add("banana"); 
  
        // define set3 to store the elements 
        // stores ["apple", "tomato", "carrot"] 
        Set<String> set3 = new HashSet<String>(); 
        set3.add("tomato"); 
        set3.add("carrot"); 
        set3.add("apple"); 
        set3.add("apple"); 
  
        // ["apple", "tomato", "carrot"] 
        System.out.println("unique Set1 elements: " + set1); 
        // ["apple", "banana", "carrot", "tomato"] 
        System.out.println("unique Set2 elements: " + set2); 
  
        // The below step prints "false" because 
        // set1 donot contains the element "banana" 
        System.out.println( 
            "Is set1 contains all the elements of set2? "
            + set1.containsAll(set2)); 
  
        // ["apple", "tomato", "carrot"] 
        System.out.println("unique Set3 elements: " + set3); 
  
        // The below step prints "true" because set1 
        // contains all the elements of set3 
        System.out.println( 
            "Is set1 contains all the elements of set3? "
            + set1.containsAll(set3)); 
    } 
}
输出
unique Set1 elements: [apple, tomato, carrot]
unique Set2 elements: [banana, apple, tomato, carrot]
Is set1 contains all the elements of set2? false
unique Set3 elements: [apple, tomato, carrot]
Is se...


相关用法


注:本文由纯净天空筛选整理自puzitha23大神的英文原创作品 Java Collection containsAll() Method with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。