The disjoint() method in Java is used to determine whether two specified collections have any elements in common. It returns true if the collections are disjoint, meaning they do not share any common elements, and false if they have at least one element in common.
Table of Contents
- Introduction
disjoint()Method Syntax- Examples
- Basic Usage of
disjoint() - Using
disjoint()with Custom Classes
- Basic Usage of
- Real-World Use Case
- Conclusion
Introduction
The Collections.disjoint() method is a static method in the java.util.Collections class. It provides a convenient way to check if two collections are completely separate or if they share any elements. This can be useful in various scenarios, such as ensuring that two datasets do not overlap or verifying that a new list of items does not contain duplicates from an existing list.
disjoint() Method Syntax
The syntax for the disjoint() method is as follows:
public static boolean disjoint(Collection<?> c1, Collection<?> c2)
Parameters:
c1: The first collection to be compared.c2: The second collection to be compared.
Returns:
trueif the two collections have no elements in common.falseif the two collections have at least one element in common.
Throws:
NullPointerExceptionif either of the collections is null.
Examples
Basic Usage of disjoint()
The following example demonstrates how to use the disjoint() method to check if two collections are disjoint.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class DisjointExample {
public static void main(String[] args) {
// Create two lists
List<String> list1 = new ArrayList<>();
Collections.addAll(list1, "Apple", "Banana", "Cherry");
List<String> list2 = new ArrayList<>();
Collections.addAll(list2, "Date", "Fig", "Grape");
// Check if the lists are disjoint
boolean areDisjoint = Collections.disjoint(list1, list2);
System.out.println("Are the lists disjoint? " + areDisjoint);
// Add a common element to the second list
list2.add("Banana");
// Check again if the lists are disjoint
areDisjoint = Collections.disjoint(list1, list2);
System.out.println("Are the lists disjoint after adding a common element? " + areDisjoint);
}
}
Output:
Are the lists disjoint? true
Are the lists disjoint after adding a common element? false
Using disjoint() with Custom Classes
You can also use the disjoint() method with collections containing instances of custom classes.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Student {
String name;
int id;
Student(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return name + " (ID: " + id + ")";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return id == student.id && name.equals(student.name);
}
@Override
public int hashCode() {
return name.hashCode() * 31 + id;
}
}
public class CustomDisjointExample {
public static void main(String[] args) {
// Create two lists of Student objects
List<Student> students1 = new ArrayList<>();
students1.add(new Student("Amit", 101));
students1.add(new Student("Neha", 102));
List<Student> students2 = new ArrayList<>();
students2.add(new Student("Raj", 103));
students2.add(new Student("Simran", 104));
// Check if the lists are disjoint
boolean areDisjoint = Collections.disjoint(students1, students2);
System.out.println("Are the student lists disjoint? " + areDisjoint);
// Add a common student to the second list
students2.add(new Student("Amit", 101));
// Check again if the lists are disjoint
areDisjoint = Collections.disjoint(students1, students2);
System.out.println("Are the student lists disjoint after adding a common student? " + areDisjoint);
}
}
Output:
Are the student lists disjoint? true
Are the student lists disjoint after adding a common student? false
Real-World Use Case
Verifying Unique Participants in Events
In real-world applications, the disjoint() method can be used to verify that two lists of participants in different events do not overlap, ensuring that each event has a unique set of attendees.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Participant {
String name;
int id;
Participant(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return name + " (ID: " + id + ")";
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Participant participant = (Participant) obj;
return id == participant.id && name.equals(participant.name);
}
@Override
public int hashCode() {
return name.hashCode() * 31 + id;
}
}
public class EventParticipantsExample {
public static void main(String[] args) {
// Participants for Event A
List<Participant> eventAParticipants = new ArrayList<>();
eventAParticipants.add(new Participant("Ravi", 201));
eventAParticipants.add(new Participant("Sita", 202));
// Participants for Event B
List<Participant> eventBParticipants = new ArrayList<>();
eventBParticipants.add(new Participant("Geeta", 203));
eventBParticipants.add(new Participant("Mohan", 204));
// Verify that participants in both events are unique
boolean areDisjoint = Collections.disjoint(eventAParticipants, eventBParticipants);
System.out.println("Are the participants for Event A and Event B disjoint? " + areDisjoint);
// Add a common participant to both events
eventBParticipants.add(new Participant("Ravi", 201));
// Verify again if participants are disjoint
areDisjoint = Collections.disjoint(eventAParticipants, eventBParticipants);
System.out.println("Are the participants for Event A and Event B disjoint after adding a common participant? " + areDisjoint);
}
}
Output:
Are the participants for Event A and Event B disjoint? true
Are the participants for Event A and Event B disjoint after adding a common participant? false
Conclusion
The Collections.disjoint() method is a useful utility for determining if two collections share any elements. By using this method, you can easily verify the uniqueness of elements across collections, which is particularly helpful in applications that require distinct datasets. Understanding and utilizing disjoint() allows you to effectively manage and analyze collections in your Java applications, ensuring that data remains consistent and accurate.