Java Collections disjoint() Method
The disjoint() method of Java Collections class is used to check whether two specified collections are disjoint or not. It returns true if the two specified collections have no elements in common.
Syntax
Following is the declaration of disjoint() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
c1 | It is the first collection. | Required |
c2 | It is the second collection. | Required |
Returns
The disjoint() method returns true if the two specified collections have no elements in common.
Exceptions
The disjoint() method throws the following exceptions-
NullPointerException– It throws this exception if either of the collection is null or if one collection contains a null element and null is not an eligible element for the other collection. (Optional).
ClassCastException– It throws this exception if one collection contains an element that is of a type which is not eligible for the other collection. (Optional).
Compatibility Version
Java 1.5 and above.
Example 1
Output:
Output: true
Example 2
Output:
Output: false
Example 3
Output:
Lists Must Have Nothing In Common! Lists Must Have Something In Common!