通过使用泛型方法和Set来表达数学中的表达式:集合的交并补。在下面三个方法中都将第一个參数Set复制了一份,并未直接改动參数中Set。package Set;
import java.util.HashSet;
import java.util.Set;
public class Sets {
public static Set intersection(Set s1, Set s2) {
Set result = new HashSet(s1);
result.retainAll(s2);
return result;
}
public static Set union(Set s1, Set s2) {
Set result = new HashSet(s1);
result.addAll(s2);
return result;
}
//Subtract subset from superset
public static Set difference (Set superset, Set subset) {
Set result = new HashSet(superset);
result.addAll(subset);
return result;
}
//Reflexive --everything not in their intersection
public static Set complement(Sets1,Set s2){
return difference(union(s1,s2),intersection(s1,s2));
}
}
原文:http://www.cnblogs.com/wzzkaifa/p/6946906.html