The Collections.disjoint() method of the java.util.Collections class is used to check whether two collections have any common elements. It returns true if the collections are completely disjoint (no common elements); otherwise, it returns false. This method works with any implementation of the Collection interface, such as List, Set, and Queue.
- Works with all Collection implementations.
- Useful for validating data overlap between collections.
import java.util.*;
public class DisjointExample {
public static void main(String[] args) {
List<String> list1 = Arrays.asList("Java", "Python", "C++");
List<String> list2 = Arrays.asList("HTML", "CSS", "JavaScript");
boolean result = Collections.disjoint(list1, list2);
System.out.println(result);
}
}
Output
true
Explanation: The two lists do not have any common elements, so Collections.disjoint() returns true.
Syntax
public static boolean disjoint(Collection<?> c1, Collection<?> c2)
Parameters
- c1: The first collection to compare.
- c2: The second collection to compare.
Return Value: Returns true if the two collections have no elements in common; otherwise, returns false.
Exceptions
- NullPointerException: Thrown if either collection is
null. - ClassCastException: May be thrown if an element in one collection cannot be compared with elements in the other collection.
Example: Checking Whether Two Collections Are Disjoint
import java.util.*;
public class GFG {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("practice");
list1.add("code");
list1.add("quiz");
list1.add("geeksforgeeks");
List<String> list2 = new Vector<>();
list2.add("geeks");
list2.add("geek");
list2.add("for");
list2.add("coder");
List<Object> list3 = new Vector<>();
list3.add(1);
list3.add("practice");
Set<String> list4 = new HashSet<>();
list4.add("practice");
list4.add("code");
list4.add("quiz");
list4.add("geeksforgeeks");
System.out.println("list1 and list2 are disjoint: "
+ Collections.disjoint(list1, list2));
System.out.println("list1 and list3 are disjoint: "
+ Collections.disjoint(list1, list3));
System.out.println("list1 and list4 are disjoint: "
+ Collections.disjoint(list1, list4));
}
}
Output
list1 and list2 are disjoint: true list1 and list3 are disjoint: false list1 and list4 are disjoint: false
Explanation: list1 and list2 have no common elements, so the method returns true. list3 and list4 both share at least one element with list1, so the method returns false.
Example: Checking Whether Two Arrays Are Disjoint
import java.util.*;
public class GFG {
public static void main(String[] args) {
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};
System.out.println("arr1 and arr2 are disjoint: "
+ Collections.disjoint(Arrays.asList(arr1),
Arrays.asList(arr2)));
System.out.println("arr1 and arr3 are disjoint: "
+ Collections.disjoint(Arrays.asList(arr1),
Arrays.asList(arr3)));
System.out.println("arr1 and arr4 are disjoint: "
+ Collections.disjoint(Arrays.asList(arr1),
Arrays.asList(arr4)));
}
}
Output
arr1 and arr2 are disjoint: true arr1 and arr3 are disjoint: false arr1 and arr4 are disjoint: true
Explanation: arr1 and arr2 have no common values, so the result is true. arr1 and arr3 both contain 50, so the result is false. Although arr1 and arr4 contain numerically similar values, Integer and Double are different types, so they are treated as different elements and the method returns true.
Advantages
- Quickly checks whether two collections have any common elements.
- Works with all implementations of the Collection interface.
- Simplifies comparison without writing nested loops.
- Improves code readability and reduces boilerplate code.