Skip to content

Commit 6dea93e

Browse files
Chapter 17 collections btp170711 (#9)
* 170537-Generic * 0528-Generic * Chapter-15-Generic-btp0530 * Chapter-16-Arrays-btp170603 * Chapter-17-Collections-btp170618 * Chapter 17 collections btp170710 * Chapter 17 collections btp170711
1 parent 4aa2c00 commit 6dea93e

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

src/btp/oneP/TypesForSets.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package btp.oneP;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class TypesForSets {
7+
static <T> Set<T> fill(Set<T> set,Class<T> type){
8+
try{
9+
for(int i=0;i<10;i++){
10+
set.add(type.getConstructor(int.class).newInstance(i));
11+
}
12+
}catch(Exception e){
13+
throw new RuntimeException(e);
14+
}
15+
return set;
16+
}
17+
18+
static <T> void test(Set<T> set,Class<T> type){
19+
fill(set,type);fill(set,type);fill(set,type);
20+
System.out.println(set);
21+
}
22+
public static void main(String[] args) {
23+
test(new HashSet<HashType>(),HashType.class);
24+
test(new HashSet<HashType>(),HashType.class);
25+
test(new HashSet<HashType>(),HashType.class);
26+
test(new HashSet<HashType>(),HashType.class);
27+
test(new HashSet<HashType>(),HashType.class);
28+
test(new HashSet<HashType>(),HashType.class);
29+
test(new HashSet<HashType>(),HashType.class);
30+
}
31+
32+
}
33+
34+
class SetType{
35+
int i;
36+
public SetType(int n){
37+
i = n;
38+
}
39+
public boolean equals(Object o){
40+
return o instanceof SetType && (i == ((SetType)o).i);
41+
}
42+
public String toString(){
43+
return Integer.toString(i);
44+
}
45+
}
46+
47+
class HashType extends SetType{
48+
49+
public HashType(int n) {
50+
super(n);
51+
// TODO Auto-generated constructor stub
52+
}
53+
54+
public int hashCode(){
55+
return i;
56+
}
57+
}
58+
59+
class TreeType extends SetType implements Comparable<TreeType>{
60+
61+
public TreeType(int n) {
62+
super(n);
63+
// TODO Auto-generated constructor stub
64+
}
65+
66+
@Override
67+
public int compareTo(TreeType o) {
68+
// TODO Auto-generated method stub
69+
return (o.i<i?-1:(o.i == i ? 0 : 1));
70+
}
71+
72+
}

0 commit comments

Comments
 (0)