94 lines
2.7 KiB
Java
Raw Normal View History

2016-12-30 22:22:39 -08:00
// collectiontopics/TypesForSets.java
2020-10-07 13:35:40 -06:00
// (c)2020 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.*;
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; }
2015-06-15 17:47:35 -07:00
@Override
public boolean equals(Object o) {
return o instanceof SetType &&
Objects.equals(i, ((SetType)o).i);
2015-06-15 17:47:35 -07:00
}
@Override
2017-01-09 14:26:12 -08:00
public String toString() {
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); }
2017-01-09 14:26:12 -08:00
@Override
public int hashCode() {
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); }
2015-06-15 17:47:35 -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]
[1, 6, 8, 6, 2, 7, 8, 9, 4, 10, 7, 5, 1, 3, 4, 9, 9,
10, 5, 3, 2, 0, 4, 1, 2, 0, 8, 3, 0, 10, 6, 5, 7]
[3, 1, 4, 8, 7, 6, 9, 5, 3, 0, 10, 5, 5, 10, 7, 8, 8,
9, 1, 4, 10, 2, 6, 9, 1, 6, 0, 3, 2, 0, 7, 2, 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, 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
*/