2015-04-20 15:36:01 -07:00
|
|
|
|
//: net/mindview/util/Sets.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
package net.mindview.util;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class Sets {
|
|
|
|
|
public static <T> Set<T> union(Set<T> a, Set<T> b) {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
Set<T> result = new HashSet<>(a);
|
2015-04-20 15:36:01 -07:00
|
|
|
|
result.addAll(b);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
public static <T>
|
|
|
|
|
Set<T> intersection(Set<T> a, Set<T> b) {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
Set<T> result = new HashSet<>(a);
|
2015-04-20 15:36:01 -07:00
|
|
|
|
result.retainAll(b);
|
|
|
|
|
return result;
|
2015-05-18 23:05:20 -07:00
|
|
|
|
}
|
2015-04-20 15:36:01 -07:00
|
|
|
|
// Subtract subset from superset:
|
|
|
|
|
public static <T> Set<T>
|
|
|
|
|
difference(Set<T> superset, Set<T> subset) {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
Set<T> result = new HashSet<>(superset);
|
2015-04-20 15:36:01 -07:00
|
|
|
|
result.removeAll(subset);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
// Reflexive--everything not in the intersection:
|
|
|
|
|
public static <T> Set<T> complement(Set<T> a, Set<T> b) {
|
|
|
|
|
return difference(union(a, b), intersection(a, b));
|
|
|
|
|
}
|
|
|
|
|
} ///:~
|