-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsTest.java
More file actions
167 lines (138 loc) · 4.19 KB
/
Copy pathGenericsTest.java
File metadata and controls
167 lines (138 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//public class GenericsTest {
// public static void main(String[] args) {
// ArrayList arr = new ArrayList();
//
// arr.add(1);
// arr.add(2);
// arr.add(3);
// arr.add("item"); // error in run time
// arr.add(4);
//
// doubleItem(arr);
// }
//
// public static void doubleItem(ArrayList n){
// for(Object i : n){
// System.out.println((Integer) i * 2);
// }
// }
//}
import org.junit.Test;
import java.util.*;
public class GenericsTest {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<> (); // <> is called diamond
arr.add(1);
arr.add(2);
arr.add(3);
// arr.add("item"); // error in compile time, way better for dev
arr.add(4);
doubleItem(arr);
//
// OrderPair<String, Integer> pair = new OrderPair<>("pwd", 123321);
// System.out.println(pair.getKey());
// System.out.println(pair.getValue());
}
public static void doubleItem(ArrayList<Integer> n){
for(int i : n){
System.out.println(i * 2);
}
}
public static class Box<T>{
private T t;
public void setT( T t){
this.t = t;
}
public T getT(){
return t;
}
public static void main(String[] args) {
Box<Integer> intBox = new Box<>();
intBox.setT(333);
System.out.println(intBox.getT());
Box<String> strBox = new Box<>();
strBox.setT("generics is quite good");
System.out.println(strBox.getT());
}
}
/* multiple type parameters */
public interface Pair<K, V> {
public K getKey();
public V getValue();
}
public static class OrderPair<K, V> implements Pair<K, V>{
public static void main(String[] args) {
OrderPair<String, Box<Integer>> pair1 = new OrderPair<>("pwd", new Box<Integer>());
pair1.getValue().setT(123);
System.out.println(pair1.getKey());
System.out.println(pair1.getValue().getT());
OrderPair<String, String> pair2 = new OrderPair<>("hello", "world");
System.out.println(pair2.getKey());
System.out.println(pair2.getValue());
}
private K key;
private V value;
public OrderPair(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
}
@Test
public void test1(){
ArrayList scores = new ArrayList();
scores.add(11);
scores.add(12);
scores.add(14);
scores.add("common"); // error in run time !!!
for(Object score : scores){
// casting may cause some problems
int stuScore = (int) score;
System.out.println(score);
}
}
// in method
@Test
public void test2(){
ArrayList<Integer> list = new ArrayList<>();
list.add(11);
list.add(12);
list.add(14);
// scores.add("common"); // error in compile time ~ ~ ~
// method 1
// for(int score : list){
// // avoid the casting
// int stuScore = score;
// System.out.println(score);
// }
// method 2
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()){
int score = iterator.next();
System.out.println(score);
}
}
// in collection
@Test
public void test3(){
Map<String, Integer> myMap = new HashMap<>();
myMap.put("Bob", 123);
myMap.put("Alex", 322);
// 泛型的嵌套
Set<Map.Entry<String, Integer>> entries = myMap.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> next = iterator.next();
String key = next.getKey();
Integer value = next.getValue();
System.out.println("key: " + key + ", value: " + value);
}
}
}