66import java .util .*;
77import java .util .function .*;
88import java .util .Objects ;
9+ import onjava .CountMap ;
910
1011class SetType {
11- int i ;
12+ protected int i ;
1213 public SetType (int n ) { i = n ; }
1314 @ Override
1415 public boolean equals (Object o ) {
1516 return o instanceof SetType &&
1617 Objects .equals (i , ((SetType )o ).i );
1718 }
1819 @ Override
19- public int hashCode () { return Objects . hash ( i ); }
20- @ Override
21- public String toString () { return Integer . toString ( i ); }
20+ public String toString () {
21+ return Integer . toString ( i );
22+ }
2223}
2324
2425class HashType extends SetType {
2526 public HashType (int n ) { super (n ); }
27+ @ Override
28+ public int hashCode () {
29+ return Objects .hashCode (i );
30+ }
2631}
2732
2833class TreeType extends SetType
2934implements Comparable <TreeType > {
3035 public TreeType (int n ) { super (n ); }
3136 @ Override
3237 public int compareTo (TreeType arg ) {
33- return (arg .i < i ? -1 : (arg .i == i ? 0 : 1 ));
38+ return Integer .compare (arg .i , i );
39+ // Equivalent to:
40+ // return arg.i < i ? -1 : (arg.i == i ? 0 : 1);
3441 }
3542}
3643
3744public class TypesForSets {
38- static <T > Set < T >
45+ static <T > void
3946 fill (Set <T > set , Function <Integer , T > type ) {
40- for (int i = 0 ; i < 10 ; i ++)
41- set .add (type .apply (i ));
42- return set ;
47+ for (int i = 10 ; i >= 5 ; i --) // Descending
48+ set .add (type .apply (i ));
49+ for (int i = 0 ; i < 5 ; i ++) // Ascending
50+ set .add (type .apply (i ));
4351 }
4452 static <T > void
4553 test (Set <T > set , Function <Integer , T > type ) {
@@ -60,23 +68,27 @@ public static void main(String[] args) {
6068 try {
6169 test (new TreeSet <>(), SetType ::new );
6270 } catch (Exception e ) {
63- System .out .println ("Expected: " + e .getMessage ());
71+ System .out .println (e .getMessage ());
6472 }
6573 try {
6674 test (new TreeSet <>(), HashType ::new );
6775 } catch (Exception e ) {
68- System .out .println ("Expected: " + e .getMessage ());
76+ System .out .println (e .getMessage ());
6977 }
7078 }
7179}
7280/* Output:
73- [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
74- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
75- [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
76- [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
77- [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
78- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
79- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
80- Expected: SetType cannot be cast to java.lang.Comparable
81- Expected: HashType cannot be cast to java.lang.Comparable
81+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
82+ [10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
83+ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
84+ [1, 6, 8, 6, 2, 7, 8, 9, 4, 10, 7, 5, 1, 3, 4, 9, 9,
85+ 10, 5, 3, 2, 0, 4, 1, 2, 0, 8, 3, 0, 10, 6, 5, 7]
86+ [3, 1, 4, 8, 7, 6, 9, 5, 3, 0, 10, 5, 5, 10, 7, 8, 8,
87+ 9, 1, 4, 10, 2, 6, 9, 1, 6, 0, 3, 2, 0, 7, 2, 4]
88+ [10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5,
89+ 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
90+ [10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5,
91+ 0, 1, 2, 3, 4, 10, 9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
92+ SetType cannot be cast to java.lang.Comparable
93+ HashType cannot be cast to java.lang.Comparable
8294*/
0 commit comments