-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericsExamples3.java
112 lines (86 loc) · 2.86 KB
/
GenericsExamples3.java
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
package concept.examples.generics;
import java.util.List;
class MyList {
private List<String> values;
void add(String value) {
values.add(value);
}
void remove(String value) {
values.remove(value);
}
}
class MyListGeneric<T> {
private List<T> values;
void add(T value) {
values.add(value);
}
void remove(T value) {
values.remove(value);
}
T get(int index) {
return values.get(index);
}
}
class MyListRestricted<T extends Number> {
private List<T> values;
void add(T value) {
values.add(value);
}
void remove(T value) {
values.remove(value);
}
T get(int index) {
return values.get(index);
}
}
public class GenericsExamples3 {
static <X extends Number> X doSomething(X number) {
X result = number;
// do something with result
return result;
}
public static void main(String[] args) {
// MyList can be used to store a list of Strings only.
MyList myList = new MyList();
myList.add("Value 1");
myList.add("Value 2");
// If I would want to create MyList accepting Integer's, I would need to
// create a new Class.
// Solution to this Problem is generics.
// Replace String with T and create a new class MyListGeneric.
// Note the declaration of the class "class MyListGeneric<T>".
// Instead of T, We can use any valid identifier(ofcourse the same
// identifier everywhere)
// If a generic is declared as part of class declaration, it can be used
// any where a type can be used in a class - method (return type or
// argument), member variable etc.
// Now the MyListGeneric class can be used to create a list of Integers
// or a list of Strings
MyListGeneric<String> myListString = new MyListGeneric<>();
myListString.add("Value 1");
myListString.add("Value 2");
MyListGeneric<Integer> myListInteger = new MyListGeneric<>();
myListInteger.add(1);
myListInteger.add(2);
// In MyListGeneric, Type P is defined as part of class declaration. Any
// Java Type can be used a type for this class.
// If we would want to restrict the types allowed for a Generic Type, we
// can use a Generic Restriction
// In declaration of the class, we specified a constraint
// "T extends Number"
// class MyListRestricted<T extends Number>
// Now, we can use the class MyListRestricted with any class extending
// Number - Float, Integer, Double etc.
// String not valid substitute for constraint "T extends Number"
// MyListRestricted<String> restrictedStringList =
// new MyListRestricted<String>();//COMPILER ERROR
MyListRestricted<Integer> restrictedListInteger = new MyListRestricted<>();
restrictedListInteger.add(1);
restrictedListInteger.add(2);
// A generic type can be declared as part of method declaration as well.
// Then the generic type can be used anywhere in the method (return
// type, parameter type, local or block variable type)
Integer i = 5;
Integer k = doSomething(i);
}
}