85 lines
2.5 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collectionsindepth/TypesForSets.java
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2016-01-25 18:05:55 -08:00
// Methods necessary to put your own type in a Set
2015-06-15 17:47:35 -07:00
import java.util.*;
2016-07-05 14:46:09 -06:00
import java.util.function.*;
2015-06-15 17:47:35 -07:00
class SetType {
int i;
public SetType(int n) { i = n; }
@Override
public boolean equals(Object o) {
return o instanceof SetType && (i == ((SetType)o).i);
}
@Override
public String toString() { return Integer.toString(i); }
}
class HashType extends SetType {
public HashType(int n) { super(n); }
@Override
public int hashCode() { return i; }
}
class TreeType extends SetType
implements Comparable<TreeType> {
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 {
2016-07-05 14:46:09 -06:00
static <T> Set<T>
fill(Set<T> set, Function<Integer, T> type) {
for(int i = 0; i < 10; i++)
set.add(type.apply(i));
2015-06-15 17:47:35 -07:00
return set;
}
2016-07-05 14:46:09 -06:00
static <T> void
test(Set<T> set, Function<Integer, T> type) {
2015-06-15 17:47:35 -07:00
fill(set, type);
fill(set, type); // Try to add duplicates
fill(set, type);
System.out.println(set);
}
public static void main(String[] args) {
2016-07-05 14:46:09 -06:00
test(new HashSet<>(), HashType::new);
test(new LinkedHashSet<>(), HashType::new);
test(new TreeSet<>(), TreeType::new);
2015-06-15 17:47:35 -07:00
// Things that don't work:
2016-07-05 14:46:09 -06:00
test(new HashSet<>(), SetType::new);
test(new HashSet<>(), TreeType::new);
test(new LinkedHashSet<>(), SetType::new);
test(new LinkedHashSet<>(), TreeType::new);
2015-06-15 17:47:35 -07:00
try {
2016-07-05 14:46:09 -06:00
test(new TreeSet<>(), SetType::new);
2015-06-15 17:47:35 -07:00
} catch(Exception e) {
System.out.println("Expected: " + e.getMessage());
}
try {
2016-07-05 14:46:09 -06:00
test(new TreeSet<>(), HashType::new);
2015-06-15 17:47:35 -07:00
} catch(Exception e) {
System.out.println("Expected: " + e.getMessage());
}
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
2016-07-22 14:45:35 -06:00
[8, 6, 3, 5, 4, 2, 2, 1, 0, 3, 6, 7, 9, 1, 3, 1, 5, 0, 9,
8, 0, 9, 8, 7, 4, 6, 2, 4, 7, 5]
[2, 2, 1, 4, 6, 6, 8, 4, 9, 3, 0, 4, 8, 5, 8, 1, 7, 5, 5,
3, 1, 9, 6, 0, 7, 0, 9, 7, 2, 3]
2015-06-15 17:47:35 -07:00
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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
2015-09-07 11:44:36 -06:00
*/