Java Collections disjoint() Method
Example
Check if two lists have no items in common:
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
ArrayList<String> cars2 = new ArrayList<String>();
cars2.add("Ford");
cars2.add("Mazda");
System.out.println(Collections.disjoint(cars, cars2));
}
}
Definition and Usage
The disjoint() method returns true if two collections have no items in common.
If at least one item is in both collections, it returns false.
Syntax
public static boolean disjoint(Collection c1, Collection c2)
Parameter Values
| Parameter | Description |
|---|---|
| c1 | Required. The first collection. |
| c2 | Required. The second collection. |
Technical Details
| Returns: | true if the collections have no items in common, otherwise false. |
|---|
Related Pages
Java Collections Framework Tutorial
❮ Collections Methods