-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMethods.java
More file actions
58 lines (45 loc) · 1.59 KB
/
Copy pathGenericMethods.java
File metadata and controls
58 lines (45 loc) · 1.59 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
public class GenericMethods {
public static void main(String[] args) {
Pair<String, Integer> p1 = new Pair<>("Hello", 22);
Pair<String, Integer> p2 = new Pair<>("Hello2", 11);
// method 1 to call compare()
System.out.println(GenericMethods.compare(p1, p2)); // false
// method 2 to call compare()
// boolean same = GenericMethods.<String, Integer>compare(p1, p2);
/*
* his feature, known as type inference, allows you to invoke a generic method as an ordinary method,
* without specifying a type between angle brackets.
* This topic is further discussed in the following section, Type Inference.*/
boolean same = GenericMethods.compare(p1, p2); // simplified syntax
System.out.println(same);
p1.setKey("Hello2");
p2.setValue(22);
System.out.println(compare(p1, p2)); // true
}
// create a generic method
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())
);
}
public static 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;
}
}
}