-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericsDemo.java
More file actions
44 lines (37 loc) · 936 Bytes
/
GenericsDemo.java
File metadata and controls
44 lines (37 loc) · 936 Bytes
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
/**
* 泛型
*/
public class GenericsDemo {
public static void main(String[] args) {
Pair<Integer, String> pair = new Pair<>(1, "apple");
Pair<Integer, String> pair1 = new Pair<>(2, "Pear");
boolean isSame;
if (compare(pair, pair1)) isSame = true;
else isSame = false;
System.out.println(isSame);
}
public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
return p1.getKey().equals(p2.getKey()) &&
p1.getValue().equals(p2.getValue());
}
}
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public void setKey(K key) {
this.key = key;
}
public void setValue(V value) {
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}