I have the following code:
Set<Set<Integer>> groups = new HashSet<>();
for (int[] edge : edges) {
Set<Integer> match1 = null;
Set<Integer> match2 = null;
for(Set<Integer> group : groups) {
if(group.contains(edge[0])) match1 = group;
if(group.contains(edge[1])) match2 = group;
}
if(match1 != null && match1 == match2) {
result = edge;
}else if(match1 != null && match2 != null && match1 != match2) {
match1.addAll(match2);
groups.remove(match2); <---- This does not remove match2 from set
}else{
Set<Integer> newGroup = new HashSet<>();
newGroup.add(edge[0]);
newGroup.add(edge[1]);
groups.add(newGroup);
}
.........
}
The groups
is a Set of Set of Integers.
However, the groups.remove(match2) method does not remove the set of integers from the groups.
What happened here?