Java

Java Collection-disjoint()

java.util.Collections.disjoint() method is used to check if two collections are disjoint or not. If both the collection objects are disjoint, true is returned; otherwise false is returned. We can say that both the collection objects are disjoint if there are no common elements.

Syntax

 

Collections.disjoint(collection_object1, collection_object2);

 

Parameters

    1. collection_object1 is the first object that refers to the collection like List, Vector etc.
    2. collection_object2 is the second object that refers to the collection like List, Vector etc.

Scenario

Consider the first ArrayList with 5 strings – [“A”,”B”,”C”,”D”,”E”] and Second ArrayList with 4 strings – [“M”,”L”, “N’”, “O”].

There are no common elements present in both the ArrayLists. We can say that these two ArrayList collections are disjoint in nature.

Example 1:

Let’s create two Vectors that hold elements of String type and check if these two collections are disjoint or not.

import java.util.*;

public class Main
{
    public static void main(String[] args) {

        //Create first vector object with String type
        Vector<String> vector_object1 = new Vector<String>();
       
        // Add 5 elements one by one into the vector_object1.
        vector_object1.add("lotus");
        vector_object1.add("rose");
        vector_object1.add("lilly");
        vector_object1.add("tulip");
        vector_object1.add("jasmine");
       
        //Elements in vector_object1
        System.out.println("First Vector: "+ vector_object1);
       
       //Create second vector object with String type
        Vector<String> vector_object2 = new Vector<String>();
       
        // Add 5 elements one by one into the vector_object2.
        vector_object2.add("potato");
        vector_object2.add("carrot");
        vector_object2.add("chillies");
        vector_object2.add("tomato");
        vector_object2.add("jasmine");
       
        //Elements in vector_object2
        System.out.println("Second Vector: "+ vector_object2);
       
        // Check whether the vector objects are disjoint or not.
        System.out.println("Do both Vectors are disjoint? : "+ Collections.disjoint(vector_object1, vector_object2));
         
        }
}

 

Output

 

“Jasmine” is present in both the Vectors. So, they are not disjoint.

Explanation

Create the first vector named vector_object1 that holds String type elements.


Line 11-15:

Now, add 5 elements into it.


Create the second vector named vector_object2 that holds String type elements.


Line 24-28:

Now, add 5 elements into it.


Line 34:

Check whether the vector objects are disjoint or not.

Example 2:

Let’s create two ArrayLists that hold elements of String type and check if these two collections are disjoint or not.

import java.util.*;

public class Main
{
    public static void main(String[] args) {

        //Create first array list object with String type
        ArrayList<String> arraylist_object1 = new ArrayList<String>();
       
        // Add 5 elements one by one into the arraylist_object1.
        arraylist_object1.add("lotus");
        arraylist_object1.add("rose");
        arraylist_object1.add("lilly");
        arraylist_object1.add("tulip");
        arraylist_object1.add("jasmine");
       
        //Elements in arraylist_object1
        System.out.println("First ArrayList: "+ arraylist_object1);
       
       //Create second array list object with String type
        ArrayList<String> arraylist_object2 = new ArrayList<String>();
       
        // Add 5 elements one by one into the arraylist_object2.
        arraylist_object2.add("potato");
        arraylist_object2.add("carrot");
        arraylist_object2.add("chillies");
        arraylist_object2.add("tomato");
        arraylist_object2.add("cotton");
       
        //Elements in arraylist_object2
        System.out.println("Second ArrayList: "+ arraylist_object2);
       
        // Check whether the array list objects are disjoint or not.
        System.out.println("Do both ArrayLists are disjoint? : "+ Collections.disjoint(arraylist_object1, arraylist_object2));
         
        }
}

 

Output

 

No element is common in both the ArrayLists. So, they are disjoint.

Explanation

Create the first arraylist named arraylist_object1 that holds String type elements.


Line 11-15:

Now, add 5 elements into it.


Create the second arraylist named vector_object2 that holds String type elements.


Line 24-28:

Now, add 5 elements into it.


Line 34:

Check whether the arraylist collection objects are disjoint or not.

Example 3:

Let’s create two ArrayLists that hold elements of Integer type and check if these two collections are disjoint or not.

Import java.util.*;

public class Main
{
    public static void main(String[] args) {

        //Create first array list object with Integer type
        ArrayList<Integer> arraylist_object1 = new ArrayList<Integer>();
       
        // Add 2 values
        arraylist_object1.add(120);
        arraylist_object1.add(220);
       
        //Elements in arraylist_object1
        System.out.println(“First ArrayList: “+ arraylist_object1);
       
       //Create second array list object with Integer type
        ArrayList<Integer> arraylist_object2 = new ArrayList<Integer>();
       
        // Add 5 elements one by one into the arraylist_object2.
        Arraylist_object2.add(7);
        arraylist_object2.add(9);
        arraylist_object2.add(6);
        arraylist_object2.add(7);
        arraylist_object2.add(9);
       
        //Elements in arraylist_object2
        System.out.println(“Second ArrayList: “+ arraylist_object2);
       
        // Check whether the array list objects are disjoint or not.
        System.out.println(“Do both ArrayLists are disjoint? : “+
        Collections.disjoint(arraylist_object1, arraylist_object2));
         
        }
}

 

Output

 

No element is common in both the ArrayLists. So, they are disjoint.

Explanation

Create the first arraylist named arraylist_object1 that holds Integer type values.


Line 11-12:

Now, add 2 values into it.


Create the second arraylist named vector_object2 that holds Integer type elements.


Line 21-25:

Now, add 5 values into it.


Line 31:

Check whether the arraylist collection objects are disjoint or not.


We can also compare different collection objects.

1. ArrayList and Vector

Output

 

2. ArrayList and LinkedList

Output

Conclusion

By the end of this article, we came to know that using the disjoint() method in java Collections which makes it possible to check for disjoint properties on two collection objects. It is also possible to compare two different collection objects like ArrayList with Vector or ArrayList with LinkedList.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain