import java.util.Set;
import java.util.TreeSet;
public class Main {
public static Set union(Set setA, Set setB) {
Set tmp = new TreeSet(setA);
tmp.addAll(setB);
return tmp;
}
public static Set intersection(Set setA, Set setB) {
Set tmp = new TreeSet();
for (T x : setA)
if (setB.contains(x))
tmp.add(x);
return tmp;
}
public static Set difference(Set setA, Set setB) {
Set tmp = new TreeSet(setA);
tmp.removeAll(setB);
return tmp;
}
public static Set symDifference(Set setA, Set setB) {
Set tmpA;
Set tmpB;
tmpA = union(setA, setB);
tmpB = intersection(setA, setB);
return difference(tmpA, tmpB);
}
public static boolean isSubset(Set setA, Set setB) {
return setB.containsAll(setA);
}
public static boolean isSuperset(Set setA, Set setB) {
return setA.containsAll(setB);
}
public static void main(String args[]) {
TreeSet set1 = new TreeSet();
TreeSet set2 = new TreeSet();
set1.add('A');
set1.add('B');
set1.add('C');
set1.add('D');
set2.add('C');
set2.add('D');
set2.add('E');
set2.add('F');
System.out.println("set1: " + set1);
System.out.println("set2: " + set2);
System.out.println("Union: " + union(set1, set2));
System.out.println("Intersection: " + intersection(set1, set2));
System.out.println("Difference (set1 - set2): " + difference(set1, set2));
System.out.println("Symmetric Difference: " + symDifference(set1, set2));
TreeSet set3 = new TreeSet(set1);
set3.remove('D');
System.out.println("set3: " + set3);
System.out.println("Is set1 a subset of set2? " + isSubset(set1, set3));
System.out.println("Is set1 a superset of set2? " + isSuperset(set1, set3));
System.out.println("Is set3 a subset of set1? " + isSubset(set3, set1));
System.out.println("Is set3 a superset of set1? " + isSuperset(set3, set1));
}
}