Given a list of objects with multiple attributes I need to find the list of sets created by a union of all intersecting subsets.
Specifically these are Person object
while (!people.isEmpty()) {
Person first = people.get(0);
people.remove(first);
Set set = makeSet(first);
for (Person person : people) {
for (Person other : set) {
if (person.isRelatedTo(other)) {
set.add(person);
people.remove(person);
}
}
}
sets.add(set);
}
for (Set a : sets) {
for (Set b : sets.except(a)) {
for (Person person : a)
for (Person other : b) {
if (person.isRelatedTo(other)) {
a.addAll(b);
b.clear();
sets.remove(b);
break;
}
}
}
}