package chapter15._4._6;
import java.util.HashSet;
import java.util.Set;
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> superset, Set<T> subset) {
Set<T> result = new HashSet<T>(superset);
result.removeAll(subset);
return result;
}
public static <T> Set<T> complement(Set<T> a, Set<T> b) {
return difference(union(a, b), intersection(a, b));
}
}
package chapter15._4._6;
public enum Watercolors {
ZINC, LEMON_YELLOW, MDDIUM_YELLOW, DEEP_YELLOW, ORANGE,
BRILLIANT_RED, CRIMSON, MAGENTA, ROSE_MADDER, VIOLET,
CERULEAN_BLUE_HUE, PHTHALO_BLUE, ULTRAMARINE,
COBALT_BLUE_HUE, PERMANENT_GREEN, VIRIDIAN_HUE,
SAP_GREEN, YELLOW_OCHRE, BURNT_SIENNA, RAW_UMBER,
BURNT_UMBER, PAYENS_GRAY, IVORY_BLACK
}
package chapter15._4._6;
import java.util.EnumSet;
import java.util.Set;
import static chapter15._4._6.Sets.*;
import static chapter15._4._6.Watercolors.*;
public class WatercolorSets {
public static void main(String[] args) {
Set<Watercolors> set1 = EnumSet.range(BRILLIANT_RED, VIRIDIAN_HUE);
Set<Watercolors> set2 = EnumSet.range(CERULEAN_BLUE_HUE, BURNT_UMBER);
System.out.println("set1: " + set1); // 11个
System.out.println("set1: " + set2); // 11个
System.out.println("union(set1, set2): " + union(set1, set2)); // 15ge
Set<Watercolors> subset = intersection(set1, set2);
System.out.println("intersection(set1, set2): " + subset); //6个
System.out.println("difference(set1, subset): " + difference(set1, subset)); // 5个
System.out.println("difference(set2, subset): " + difference(set2, subset)); // 5个
System.out.println("difference(set1, sub2): " + difference(set1, set2)); // 5个
System.out.println("complement(set1, set2): " + complement(set1, set2)); // 10个
}
}
输出
set1: [BRILLIANT_RED, CRIMSON, MAGENTA, ROSE_MADDER, VIOLET, CERULEAN_BLUE_HUE, PHTHALO_BLUE, ULTRAMARINE, COBALT_BLUE_HUE, PERMANENT_GREEN, VIRIDIAN_HUE]
set1: [CERULEAN_BLUE_HUE, PHTHALO_BLUE, ULTRAMARINE, COBALT_BLUE_HUE, PERMANENT_GREEN, VIRIDIAN_HUE, SAP_GREEN, YELLOW_OCHRE, BURNT_SIENNA, RAW_UMBER, BURNT_UMBER]
union(set1, set2): [PHTHALO_BLUE, MAGENTA, ULTRAMARINE, VIOLET, BURNT_SIENNA, VIRIDIAN_HUE, CERULEAN_BLUE_HUE, SAP_GREEN, YELLOW_OCHRE, PERMANENT_GREEN, CRIMSON, RAW_UMBER, BURNT_UMBER, BRILLIANT_RED, ROSE_MADDER, COBALT_BLUE_HUE]
intersection(set1, set2): [PHTHALO_BLUE, ULTRAMARINE, PERMANENT_GREEN, VIRIDIAN_HUE, CERULEAN_BLUE_HUE, COBALT_BLUE_HUE]
difference(set1, subset): [MAGENTA, CRIMSON, VIOLET, BRILLIANT_RED, ROSE_MADDER]
difference(set2, subset): [SAP_GREEN, YELLOW_OCHRE, BURNT_SIENNA, RAW_UMBER, BURNT_UMBER]
difference(set1, sub2): [MAGENTA, CRIMSON, VIOLET, BRILLIANT_RED, ROSE_MADDER]
complement(set1, set2): [MAGENTA, VIOLET, BURNT_SIENNA, SAP_GREEN, YELLOW_OCHRE, CRIMSON, RAW_UMBER, BURNT_UMBER, BRILLIANT_RED, ROSE_MADDER]
package chapter15._4._6;
import java.lang.reflect.Method;
import java.util.*;
public class ContainerMethodDifferences {
static Set<String> methodSet(Class<?> type) {
Set<String> result = new TreeSet<String>();
for(Method m : type.getMethods())
result.add(m.getName());
return result;
}
static void interfaces(Class<?> type) {
System.out.print("Interfaces in " + type.getSimpleName() + ": ");
List<String> result = new ArrayList<String>();
for(Class<?> c : type.getInterfaces())
result.add(c.getSimpleName());
System.out.println(result);
}
static Set<String> object = methodSet(Object.class);
static {
object.add("clone");
}
static void difference(Class<?> superset, Class<?> subset) {
System.out.println();
System.out.print(superset.getSimpleName() + " extends " + subset.getSimpleName() + ", add: ");
Set<String> comp = Sets.difference(methodSet(superset), methodSet(subset));
comp.removeAll(object);
System.out.println(comp);
interfaces(superset);
}
public static void main(String[] args) {
System.out.println("Collection: " + methodSet(Collection.class));
interfaces(Collection.class);
difference(Set.class, Collection.class);
difference(HashSet.class, Set.class);
difference(LinkedHashSet.class, HashSet.class);
difference(TreeSet.class, Set.class);
difference(List.class, Collection.class);
difference(ArrayList.class, List.class);
difference(LinkedList.class, List.class);
difference(Queue.class, Collection.class);
difference(PriorityQueue.class, Queue.class);
System.out.println();
System.out.println("Map: " + methodSet(Map.class));
difference(HashMap.class, Map.class);
difference(LinkedHashMap.class, HashMap.class);
difference(SortedMap.class, Map.class);
difference(TreeMap.class, Map.class);
}
}
输出
Collection: [add, addAll, clear, contains, containsAll, equals, forEach, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray]
Interfaces in Collection: [Iterable]
Set extends Collection, add: []
Interfaces in Set: [Collection]
HashSet extends Set, add: []
Interfaces in HashSet: [Set, Cloneable, Serializable]
LinkedHashSet extends HashSet, add: []
Interfaces in LinkedHashSet: [Set, Cloneable, Serializable]
TreeSet extends Set, add: [headSet, descendingIterator, descendingSet, pollLast, subSet, floor, tailSet, ceiling, last, lower, comparator, pollFirst, first, higher]
Interfaces in TreeSet: [NavigableSet, Cloneable, Serializable]
List extends Collection, add: [replaceAll, get, indexOf, subList, set, sort, lastIndexOf, listIterator]
Interfaces in List: [Collection]
ArrayList extends List, add: [trimToSize, ensureCapacity]
Interfaces in ArrayList: [List, RandomAccess, Cloneable, Serializable]
LinkedList extends List, add: [offerFirst, poll, getLast, offer, getFirst, removeFirst, element, removeLastOccurrence, peekFirst, peekLast, push, pollFirst, removeFirstOccurrence, descendingIterator, pollLast, removeLast, pop, addLast, peek, offerLast, addFirst]
Interfaces in LinkedList: [List, Deque, Cloneable, Serializable]
Queue extends Collection, add: [poll, peek, offer, element]
Interfaces in Queue: [Collection]
PriorityQueue extends Queue, add: [comparator]
Interfaces in PriorityQueue: [Serializable]
Map: [clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, replace, replaceAll, size, values]
HashMap extends Map, add: []
Interfaces in HashMap: [Map, Cloneable, Serializable]
LinkedHashMap extends HashMap, add: []
Interfaces in LinkedHashMap: [Map]
SortedMap extends Map, add: [lastKey, subMap, comparator, firstKey, headMap, tailMap]
Interfaces in SortedMap: [Map]
TreeMap extends Map, add: [descendingKeySet, navigableKeySet, higherEntry, higherKey, floorKey, subMap, ceilingKey, pollLastEntry, firstKey, lowerKey, headMap, tailMap, lowerEntry, ceilingEntry, descendingMap, pollFirstEntry, lastKey, firstEntry, floorEntry, comparator, lastEntry]
Interfaces in TreeMap: [NavigableMap, Cloneable, Serializable]