// onjava/Sets.java // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. package onjava; import java.util.*; public class Sets { public static Set union(Set a, Set b) { Set result = new HashSet<>(a); result.addAll(b); return result; } public static Set intersection(Set a, Set b) { Set result = new HashSet<>(a); result.retainAll(b); return result; } // Subtract subset from superset: public static Set difference(Set superset, Set subset) { Set result = new HashSet<>(superset); result.removeAll(subset); return result; } // Reflexive--everything not in the intersection: public static Set complement(Set a, Set b) { return difference(union(a, b), intersection(a, b)); } }