java.util.Collections類中提供了java.util.Collections.disjoint()方法。它用於檢查兩個指定的集合是否不相交。更正式地講,如果兩個集合沒有共同的元素,則它們是不相交的。
用法: public static boolean disjoint(Collection<?> c1, Collection<?> c2) 參數: c1 - a collection c2 - a collection 返回: true if the two specified collections have no elements in common. Throws: NullPointerException - if either collection is null. NullPointerException - if one collection contains a null element and null is not an eligible element for the other collection. ClassCastException - if one collection contains an element that is of a type which is ineligible for the other collection.
注意,允許在兩個參數中傳遞相同的集合,在這種情況下,當且僅當集合為空時,該方法才會返回true。
// Java program to demonstrate working of
// java.utils.Collections.disjoint()
import java.util.*;
public class DisjointDemo
{
public static void main(String[] args)
{
// Let us create array list of strings
List<String> mylist1 = new ArrayList<String>();
mylist1.add("practice");
mylist1.add("code");
mylist1.add("quiz");
mylist1.add("geeksforgeeks");
// Let us create vector of strings
List<String> mylist2 = new Vector<String>();
mylist2.add("geeks");
mylist2.add("geek");
mylist2.add("for");
mylist2.add("coder");
// Let us create a vector
List mylist3 = new Vector();
mylist3.add(1);
mylist3.add("practice");
// Let us create a Set of strings
Set<String> mylist4 = new HashSet<String>();
mylist4.add("practice");
mylist4.add("code");
mylist4.add("quiz");
mylist4.add("geeksforgeeks");
// Here we are using disjoint() method to check
// whether two collections are disjoint or not
System.out.println("is mylist1 disjoint to mylist2:" +
Collections.disjoint(mylist1, mylist2));
System.out.println("is mylist1 disjoint to mylist3:" +
Collections.disjoint(mylist1, mylist3));
System.out.println("is mylist1 disjoint to mylist4:" +
Collections.disjoint(mylist1, mylist4));
}
}
輸出:
is mylist1 disjoint to mylist2:true is mylist1 disjoint to mylist3:false is mylist1 disjoint to mylist4:false
How to quickly check whether two arrays in Java are disjoint or not?
Java中的Arrays類沒有脫節方法。我們可以使用Collections.disjoint()快速檢查兩個數組的不相交性。
// Java program to demonstrate disjoint
// method with arrays
import java.util.*;
public class DisjointDemo
{
public static void main(String[] args)
{
// Let us create arrays of integers
Integer arr1[] = {10, 20, 30, 40, 50};
Integer arr2[] = {60, 70, 80, 90, 100};
Integer arr3[] = {50, 70, 80, 90, 100};
Double arr4[] = {50.0, 60.0, 110.0};
// Please refer below post for details of asList()
// https://www.geeksforgeeks.org/array-class-in-java/
// Here we are using disjoint() method to check
// whether two arrays are disjoint or not
System.out.println("is arr1 disjoint to arr2:" +
Collections.disjoint(Arrays.asList(arr1), Arrays.asList(arr2)));
System.out.println("is arr1 disjoint to arr3:" +
Collections.disjoint(Arrays.asList(arr1), Arrays.asList(arr3)));
System.out.println("is arr1 disjoint to arr4:" +
Collections.disjoint(Arrays.asList(arr1), Arrays.asList(arr4)));
}
}
輸出:
is arr1 disjoint to arr2:true is arr1 disjoint to arr3:false is arr1 disjoint to arr4:true
相關用法
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Set contains()用法及代碼示例
- Java Set add()用法及代碼示例
- Java Map get()用法及代碼示例
注:本文由純淨天空篩選整理自 Java.util.Collections.disjoint() Method in java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。