2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/TypesForSets.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 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 {
|
2017-01-09 14:26:12 -08:00
|
|
|
protected int i;
|
2017-05-01 14:33:10 -06:00
|
|
|
SetType(int n) { i = n; }
|
2021-01-31 15:42:31 -07:00
|
|
|
@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
|
|
|
}
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public String toString() {
|
2017-01-09 14:26:12 -08:00
|
|
|
return Integer.toString(i);
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class HashType extends SetType {
|
2017-05-01 14:33:10 -06:00
|
|
|
HashType(int n) { super(n); }
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public int hashCode() {
|
2017-01-09 14:26:12 -08:00
|
|
|
return Objects.hashCode(i);
|
|
|
|
}
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class TreeType extends SetType
|
|
|
|
implements Comparable<TreeType> {
|
2017-05-01 14:33:10 -06:00
|
|
|
TreeType(int n) { super(n); }
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public int compareTo(TreeType arg) {
|
2017-01-09 14:26:12 -08:00
|
|
|
return Integer.compare(arg.i, i);
|
|
|
|
// Equivalent to:
|
|
|
|
// return arg.i < i ? -1 : (arg.i == i ? 0 : 1);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TypesForSets {
|
2017-01-09 14:26:12 -08:00
|
|
|
static <T> void
|
2016-07-05 14:46:09 -06:00
|
|
|
fill(Set<T> set, Function<Integer, T> type) {
|
2017-01-09 14:26:12 -08:00
|
|
|
for(int i = 10; i >= 5; i--) // Descending
|
|
|
|
set.add(type.apply(i));
|
|
|
|
for(int i = 0; i < 5; i++) // Ascending
|
|
|
|
set.add(type.apply(i));
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
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) {
|
2017-01-09 14:26:12 -08:00
|
|
|
System.out.println(e.getMessage());
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
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) {
|
2017-01-09 14:26:12 -08:00
|
|
|
System.out.println(e.getMessage());
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-01-09 14:26:12 -08:00
|
|
|
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
|
[10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
|
|
|
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
2021-01-31 15:42:31 -07:00
|
|
|
[4, 1, 5, 1, 0, 5, 6, 8, 7, 0, 2, 8, 4, 9, 6, 10, 6, 7,
|
|
|
|
2, 9, 10, 3, 8, 4, 10, 3, 9, 5, 3, 7, 1, 2, 0]
|
|
|
|
[9, 8, 4, 8, 5, 0, 1, 6, 2, 9, 3, 7, 2, 2, 7, 0, 9, 5,
|
|
|
|
5, 6, 4, 7, 10, 1, 6, 4, 1, 10, 3, 3, 0, 10, 8]
|
2017-05-10 11:45:39 -06:00
|
|
|
[10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5,
|
|
|
|
0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
|
|
|
[10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5,
|
|
|
|
0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
|
2017-01-09 14:26:12 -08:00
|
|
|
SetType cannot be cast to java.lang.Comparable
|
|
|
|
HashType cannot be cast to java.lang.Comparable
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|