Java.util.Collections.disjoint() Method in java with Examples

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.

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.
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.

// 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>();
        // Let us create vector of strings
        List<String>  mylist2 = new Vector<String>();
        // Let us create a vector 
        List mylist3 = new Vector();
        // Let us create a Set of strings
        Set<String>  mylist4 = new HashSet<String>();
        // 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?

Arrays class in Java doesn’t have disjoint method. We can use Collections.disjoint() to check quickly disjoincy of two arrays.

// 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()
        // 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

This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.

My Personal Notes arrow_drop_up

Practice Tags :

Recommended Posts:

2 Average Difficulty : 2/5.0
Based on 1 vote(s)

User Actions