i want to merge two set element in one.
public static <T> Set<Set<Long>> merge(Set<Set<Long>> splitSets, long a, long b) {
Iterator<Set<Long>> it1 = splitSets.iterator();
for (long i=0; i<splitSets.size(); i++) {
HashSet<Long> s1=null,s2 = null,s3 = null;
for (long j=0; it1.hasNext(); j++) {
s1=(LinkedHashSet<Long>) it1.next();
if(s1.contains(a)){
s2=s1;
}
if(s1.contains(b)){
s3=s1;
}
}
if(s2!=null && s3!=null){
s2.addAll(s3);
splitSets.remove(s3);
}
}
return splitSets;
}
i have write the above code for merging the inner set element of the outer set. if given value of a and b found in two different set it has to merge into one. it work while there is single element in any of the set but if both set contain more than one element then it merge into first but do not remove the second please help me to find problem.
following are the call of merge method and corresponding output.
original splitSet
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
splitSets = merge(splitSets,3,5);
[[1], [2], [3, 5], [4], [6], [7], [8], [9], [10]]
splitSets = merge(splitSets,2,6);
[[1], [2, 6], [3, 5], [4], [7], [8], [9], [10]]
splitSets = merge(splitSets,6,3);
[[1], [2, 6, 3, 5], [3, 5], [4], [7], [8], [9], [10]]