java.util.Collections.disjoint() method is present in java.util.Collections class. It is used to check whether two specified collections are disjoint or not. More formally, two collections are disjoint if they have no elements in common.
Syntax: public static boolean disjoint(Collection<?> c1, Collection<?> c2) Parameters : c1 - a collection c2 - a collection Returns : 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.
Note that it is permissible to pass the same collection in both parameters, in which case the method will return true if and only if the collection is empty.
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?
Arrays class in Java doesn’t have disjoint method. We can use Collections.disjoint() to check quickly disjoincy of two arrays.
is arr1 disjoint to arr2 : true is arr1 disjoint to arr3 : false is arr1 disjoint to arr4 : true
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java.util.Collections.rotate() Method in Java with Examples
- Java 8 | ArrayDeque removeIf() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.frequency() in Java with Examples
- Java.util.Arrays.equals() in Java with Examples
- Java 8 | Consumer Interface in Java with Examples
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java 8 | BiConsumer Interface in Java with Examples