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

ParameterDescriptionRequired/Optional
c1It is the first collection.Required
c2It 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

Test it Now

Output:

Output: true

Example 2

Test it Now

Output:

Output: false

Example 3

Test it Now

Output:

Lists Must Have Nothing In Common!
Lists Must Have Something In Common!





Latest Courses