集合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...
相關用法
- Java Collection containsAll()用法及代碼示例
- Java Collection contains()用法及代碼示例
- Java Collection clear()用法及代碼示例
- Java Collection equals()用法及代碼示例
- Java Collection hashCode()用法及代碼示例
- Java Collection iterator()用法及代碼示例
- Java Collection remove()用法及代碼示例
- Java Collection removeAll()用法及代碼示例
- Java Collection removeIf()用法及代碼示例
- Java Collection retainAll()用法及代碼示例
- Java Collection size()用法及代碼示例
- Java Collection spliterator()用法及代碼示例
- Java Collection toArray()用法及代碼示例
- Java Collection add()用法及代碼示例
- Java Collection addAll()用法及代碼示例
- Java Collection isEmpty()用法及代碼示例
- Java Collection Interface用法及代碼示例
- Java Collections binarySearch()用法及代碼示例
- Java Collections checkedNavigableMap()用法及代碼示例
- Java Collections checkedNavigableSet()用法及代碼示例
- Java Collections checkedQueue()用法及代碼示例
- Java Collections disjoint()用法及代碼示例
- Java Collections emptyEnumeration()用法及代碼示例
- Java Collections emptyIterator()用法及代碼示例
- Java Collections emptyList()用法及代碼示例
注:本文由純淨天空篩選整理自puzitha23大神的英文原創作品 Java Collection containsAll() Method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。