2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/TypesForSets.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 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.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com 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.*;
|
2016-11-21 12:37:57 -08:00
|
|
|
import java.util.Objects;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
class SetType {
|
|
|
|
int i;
|
|
|
|
public SetType(int n) { i = n; }
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
2016-11-21 12:37:57 -08:00
|
|
|
return o instanceof SetType &&
|
|
|
|
Objects.equals(i, ((SetType)o).i);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
@Override
|
2016-11-21 12:37:57 -08:00
|
|
|
public int hashCode() { return Objects.hash(i); }
|
|
|
|
@Override
|
2015-06-15 17:47:35 -07:00
|
|
|
public String toString() { return Integer.toString(i); }
|
|
|
|
}
|
|
|
|
|
|
|
|
class HashType extends SetType {
|
|
|
|
public HashType(int n) { super(n); }
|
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2016-11-21 12:37:57 -08:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
|
2015-06-15 17:47:35 -07:00
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
2016-11-21 12:37:57 -08:00
|
|
|
[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]
|
2015-06-15 17:47:35 -07:00
|
|
|
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
|
|
|
*/
|