The Wayback Machine - https://web.archive.org/web/20240708202321/https://www.geeksforgeeks.org/java-util-collections-disjoint-method-java-examples/
Open In App

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

Last Updated : 07 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.




// 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>();
        mylist1.add("practice");
        mylist1.add("code");
        mylist1.add("quiz");
        mylist1.add("geeksforgeeks");
          
        // Let us create vector of strings
        List<String>  mylist2 = new Vector<String>();
        mylist2.add("geeks");
        mylist2.add("geek");
        mylist2.add("for");
        mylist2.add("coder");
          
        // Let us create a vector 
        List mylist3 = new Vector();
          
        mylist3.add(1); 
        mylist3.add("practice");     
          
        // Let us create a Set of strings
        Set<String>  mylist4 = new HashSet<String>();
        mylist4.add("practice");
        mylist4.add("code");
        mylist4.add("quiz");
        mylist4.add("geeksforgeeks");
          
          
        // 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));
  
    }
}


Output:

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)));
  
    }
}


Output:

is arr1 disjoint to arr2 : true
is arr1 disjoint to arr3 : false
is arr1 disjoint to arr4 : true


Similar Reads

Java.util.Collections.rotate() Method in Java with Examples
java.util.Collections.rotate() method is present in java.util.Collections class. It is used to rotate the elements present in the specified list of Collection by a given distance. Syntax: public static void rotate(List&lt; type &gt; list, int distance) Parameters : list - the list to be rotated. distance - the distance to rotate the list. type - Ty
2 min read
Java.util.Collections.frequency() in Java
The method is a java.util.Collections class method. It counts the frequency of the specified element in the given list. It override the equals() method to perform the comparison to check if the specified Object and the Object in the list are equal or not. Syntax: public static int frequency(Collection c, Object o) parameters: c: Collection in which
2 min read
Difference between Traditional Collections and Concurrent Collections in java
We all know about Traditional Collections ( i.e. List, Set, Queue and its implemented Classes) and Concurrent Collection (i.e. ConcurrentMap interface, ConcurrentHashMap class, CopyOnWriteArrayList class etc). In these two Collections, there are few differences like: Most of the Classes which are present in Traditional Collections (i.e ArrayList, L
3 min read
Collections checkedMap() method in Java with Examples
The checkedMap() method of Collections class been present inside java.util package is used to return a dynamically typesafe view of the specified map. The returned map will be serializable if the specified map is serializable. Since null is considered to be a value of any reference type, the returned map permits the insertion of null keys or values
3 min read
Collections singletonMap() method in Java with Examples
The singletonMap() method of java.util.Collections class is used to return an immutable map, mapping only the specified key to the specified value. The returned map is serializable. Syntax: public static Map singletonMap(K key, V value) Parameters: This method takes the following parameters as a argument: key - the sole key to be stored in the retu
2 min read
Collections min() method in Java with Examples
min(Collection&lt;? extends T&gt; coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (tha
4 min read
Collections max() method in Java with Examples
max(Collection&lt;? extends T&gt; coll) The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements in the collection must be mutually comparable (tha
5 min read
Collections addAll() method in Java with Examples
The addAll() method of java.util.Collections class is used to add all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under
3 min read
Collections asLifoQueue() method in Java with Examples
The asLifoQueue() method of java.util.Collections class is used to return a view of a Deque as a Last-in-first-out (Lifo) Queue. Method add is mapped to push, remove is mapped to pop and so on. This view can be useful when you would like to use a method requiring a Queue but you need Lifo ordering.Each method invocation on the queue returned by thi
2 min read
Collections checkedCollection() method in Java with Examples
The checkedCollection() method of java.util.Collections class is used to return a dynamically typesafe view of the specified collection. The returned collection does not pass the hashCode and equals operations through to the backing collection, but relies on Object's equals and hashCode methods. This is necessary to preserve the contracts of these
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg