I want to write a method that uses the standard for loop to iterate through a given set of sets, and returns a hashmap that the disjoint sets are added to. I'm aware that the does not work correctly, it's an example of how I've tried to solve this. How do I check the elements within the sets, also without the algorithm becoming too inefficient?
public <E> Map<Set<E>, Set<Set<E>>> disjointSets(Set<Set<E>> Sets) {
Map<Set<E>, Set<Set<E>>> result = new HashMap<>();
for (Set<E> x : Sets) {
for (Set<E> y : Sets) {
if (!x.equals(y)) {
result.put(x, Sets);
}
}
}
return result;
}
/**
* As an example, if input sets would be:
* [0]
* [0, 4, 5]
* [1, 2]
* -> expected output should be:
* [0] : [ [1, 2] ]
* [0, 4, 5] : [ [1, 2] ]
* [1, 2] : [ [0] [0, 4, 5] ]
*/
You can just cycle trough them and use Set.contains