// understandingcollections/TypesForSets.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. // Methods necessary to put your own type in a Set import java.util.*; import java.util.function.*; import java.util.Objects; class SetType { int i; public SetType(int n) { i = n; } @Override public boolean equals(Object o) { if (o == this) return true; return o instanceof SetType && Objects.equals(i, ((SetType)o).i); } @Override public int hashCode() { return Objects.hash(i); } @Override public String toString() { return Integer.toString(i); } } class HashType extends SetType { public HashType(int n) { super(n); } } class TreeType extends SetType implements Comparable { public TreeType(int n) { super(n); } @Override public int compareTo(TreeType arg) { return (arg.i < i ? -1 : (arg.i == i ? 0 : 1)); } } public class TypesForSets { static Set fill(Set set, Function type) { for(int i = 0; i < 10; i++) set.add(type.apply(i)); return set; } static void test(Set set, Function type) { fill(set, type); fill(set, type); // Try to add duplicates fill(set, type); System.out.println(set); } public static void main(String[] args) { test(new HashSet<>(), HashType::new); test(new LinkedHashSet<>(), HashType::new); test(new TreeSet<>(), TreeType::new); // Things that don't work: test(new HashSet<>(), SetType::new); test(new HashSet<>(), TreeType::new); test(new LinkedHashSet<>(), SetType::new); test(new LinkedHashSet<>(), TreeType::new); try { test(new TreeSet<>(), SetType::new); } catch(Exception e) { System.out.println("Expected: " + e.getMessage()); } try { test(new TreeSet<>(), HashType::new); } catch(Exception e) { System.out.println("Expected: " + e.getMessage()); } } } /* Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Expected: SetType cannot be cast to java.lang.Comparable Expected: HashType cannot be cast to java.lang.Comparable */