-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionNewFeatures.java
More file actions
140 lines (101 loc) · 2.65 KB
/
CollectionNewFeatures.java
File metadata and controls
140 lines (101 loc) · 2.65 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
package Codes;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
class SortByNamePerson implements Comparator<Person1> {
@Override
public int compare(Person1 o1, Person1 o2) {
// TODO Auto-generated method stub
return o1.name.compareTo(o2.name);
}
}
class Person1 implements Comparable<Person1> {
String name;
int age;
Person1(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person1 o) {
// TODO Auto-generated method stub
return this.name.compareTo(o.name);
// return this.age-o.age;
}
@Override
public String toString() {
return "Person1 [name=" + name + ", age=" + age + "]";
}
}
// ***************************************************
class P {
}
class A11 extends P {
}
class B extends A11 {
}
class C extends A11 {
}
class K {
}
// Wild Card - The Limitation of Generic is to hold only one type
// to overcome with this problem the solution is Wild Card
public class CollectionNewFeatures {
static void print2(ArrayList list) {
}
static void arrayExample(A11 x[]) {
}
static void print(ArrayList<A11> list) {
}
static void nonGeneric(ArrayList list) {
list.add("Hello");
list.add(10);
list.add(20.90);
}
static void wildCard(ArrayList<?> list) {
}
static void wildCardSuper(ArrayList<? super A11> list) {
}
static void wildCardExtends(ArrayList<? extends A11> list) {
}
static void wildCardExtends2(ArrayList<? extends Serializable> list) {
}
public static void main(String[] args) {
C c[] = new C[10];
arrayExample(c);
ArrayList<P> pList = new ArrayList<>();
ArrayList<A11> aList = new ArrayList<>();
ArrayList<C> cList = new ArrayList<>();
ArrayList<K> kList = new ArrayList<>();
cList.add(new C());
wildCardExtends(cList);
wildCardSuper(pList);
wildCard(pList);
wildCard(aList);
wildCard(cList);
wildCard(kList);
ArrayList nonGenericList = new ArrayList();
nonGenericList.add(new C());
nonGenericList.add(new B());
nonGenericList.add(new A11());
nonGenericList.add(new K());
nonGeneric(nonGenericList);
// wildCardExtends(kList);
// cList.add(new B());
/*
* Queue<Person1> queue = new PriorityQueue<>(new SortByNamePerson());
* queue.add(new Person1("ram",21)); queue.add(new Person1("shyam",22));
* queue.add(new Person1("mike",23)); queue.add(new
* Person1("sohan",24)); System.out.println(queue);
*/
// Generics
ArrayList<Integer> intList = new ArrayList<>();
intList.add(10);
// intList.add(20.20);
ArrayList list = new ArrayList();
list.add(10); // list.add(new Integer(10));
list.add("Hello");
list.add(20.90);
double d = (double) list.get(2);
}
}