forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListDemo.java
More file actions
188 lines (137 loc) · 5.06 KB
/
ArrayListDemo.java
File metadata and controls
188 lines (137 loc) · 5.06 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ArrayListDemo;
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args) {
// createArrayListThenAddElement();
// createArrayListFromOtherArrayList();
// arrayListRemoveDemo();
// iteratingListRemoveDemo();
// sortArrayListDemo();
// usingArrayListSortMethod();
usingComparableInterface();
}
public static void createArrayListThenAddElement() {
ArrayList<String> carArrayList = new ArrayList();
carArrayList.add("BMW");
carArrayList.add("Benz");
carArrayList.add("VV");
System.out.println(carArrayList);
System.out.println(carArrayList.size());
System.out.println(carArrayList.isEmpty());
System.out.println(carArrayList.get(0));
}
public static void createArrayListFromOtherArrayList() {
ArrayList<String> benzCarArrayList = new ArrayList();
benzCarArrayList.add("GLE");
benzCarArrayList.add("smart");
benzCarArrayList.add("GLC");
ArrayList<ArrayList<String>> carArrayList = new ArrayList();
carArrayList.add(benzCarArrayList);
System.out.println(carArrayList);
}
public static void arrayListRemoveDemo() {
ArrayList<String> carArrayList = new ArrayList();
carArrayList.add("BMW");
carArrayList.add("Benz");
carArrayList.add("VV");
carArrayList.remove(0);
carArrayList.remove("Benz");
carArrayList.removeAll(carArrayList);
System.out.println(carArrayList);
}
public static void iteratingListRemoveDemo() {
ArrayList<String> carArrayList = new ArrayList();
carArrayList.add("BMW");
carArrayList.add("Benz");
carArrayList.add("VV");
System.out.println("---------foreach-------------");
for (String item : carArrayList) {
System.out.println(item);
}
System.out.println("---------iterator-------------");
Iterator<String> itr = carArrayList.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("----------simple for------------");
for (int i = 0; i < carArrayList.size(); i++) {
System.out.println(carArrayList.get(i));
}
}
public static void sortArrayListDemo() {
ArrayList<Integer> carArrayList = new ArrayList();
carArrayList.add(43);
carArrayList.add(-1);
carArrayList.add(33);
carArrayList.add(1);
System.out.println(carArrayList);
System.out.println("After sorted");
Collections.sort(carArrayList);
System.out.println(carArrayList);
}
public static void usingArrayListSortMethod() {
List<String> names = new ArrayList<>();
names.add("Lisa");
names.add("Jennifer");
names.add("Mark");
names.add("David");
System.out.println("Names : " + names);
// Sort an ArrayList using its sort() method. You must pass a Comparator to the ArrayList.sort() method.
names.sort(new Comparator<String>() {
@Override
public int compare(String name1, String name2) {
return name1.compareTo(name2);
}
});
// The above `sort()` method call can also be written simply using lambda expression
names.sort((name1, name2) -> name1.compareTo(name2));
// Following is an even more concise solution
names.sort(Comparator.naturalOrder());
System.out.println("Sorted Names : " + names);
}
public static void usingComparableInterface() {
List<Person> people = new ArrayList<>();
people.add(new Person("Sachin", 47));
people.add(new Person("Chris", 34));
people.add(new Person("Rajeev", 25));
people.add(new Person("David", 31));
System.out.println("Person List : " + people);
// Sort People by their Age
people.sort((person1, person2) -> {
return person1.getAge() - person2.getAge();
});
// A more concise way of writing the above sorting function
people.sort(Comparator.comparingInt(Person::getAge));
System.out.println("Sorted Person List by Age : " + people);
// You can also sort using Collections.sort() method by passing the custom Comparator
Collections.sort(people, Comparator.comparing(Person::getName));
System.out.println("Sorted Person List by Name : " + people);
}
}
class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}