import javafx.animation.SequentialTransition; import java.util.*; public class Sets { //求并集 public static <T> Set<T> union(Set<T> a, Set<T> b) { Set<T> result = new HashSet<T>(a); result.addAll(b); return result; } //求交集 public static <T> Set<T> intersection(Set<T> a, Set<T> b) { Set<T> result = new HashSet<T>(a); result.retainAll(b); return result; } //求差集 public static <T> Set<T> difference(Set<T> a, Set<T> b) { Set<T> result = new HashSet<>(a); result.removeAll(b); return result; } //求交集外所有元素 public static <T> Set<T> complement(Set<T> a, Set<T> b) { return difference(union(a,b),intersection(a,b)); } }
用Set实现数学集合的交并差,交集外所有的元素
最新推荐文章于 2024-05-20 17:41:15 发布