forked from Rapter1990/JavaStreamAPIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterConsumerPredicateExample.java
More file actions
312 lines (216 loc) · 9.38 KB
/
FilterConsumerPredicateExample.java
File metadata and controls
312 lines (216 loc) · 9.38 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package functionalexamples;
import employeedata.Designation;
import employeedata.Employee;
import employeedata.Skill;
import employeedata.Unit;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingInt;
public class FilterConsumerPredicateExample {
public static void main(String args[]) {
// 1. Get a list of all employees in EDC.
testEDCFilter();
// 2.Get all the java developers.
testJavaDevelopers();
// 3. Get all senior professionals
testSeniorProfessionals();
// 4. Java developer in edc. Implement here.
testJavaDevelopersInEDC();
// 5. Sort By Name
sortByName();
// 6. Sort By InreasingExp and Name
sortByInreasingExpAndThenName();
// 7. Sort By DecreasingExp and Name
sortByDecreasingExpAndThenName();
// 8. get employees compared with their names
getEmployees(Comparator.comparing(Employee::getName));
}
private static void testJavaDevelopersInEDC() {
List<Employee> javaEdcEmployees = getEmployeesFilteredBy(edcFilter().and(javaFilter()));
System.out.println("--------Java Developers in EDC--------");
System.out.println(javaEdcEmployees);
/*
[Amit with 8 years of experience
]
*/
}
private static void testSeniorProfessionals() {
int years = 10;
List<Employee> seniorProfessionals = getEmployeesFilteredBy(seniorProfessionals(years));
System.out.println("--------Senior Professionals-------");
System.out.println(seniorProfessionals);
/*
[Jose with 15 years of experience
, Pirlo with 13 years of experience
]
*/
}
private static void testJavaDevelopers() {
List<Employee> javaDevelopers = getEmployeesFilteredBy(employee -> employee.getSkills().contains(Skill.JAVA));
System.out.println("--------Java Developers--------");
System.out.println(javaDevelopers);
/*
[
Amit with 8 years of experience,
Manoj with 3 years of experience
]
*/
}
private static void testEDCFilter() {
List<Employee> edcEmployees = getEmployeesFilteredBy(edcFilter());
System.out.println("--------EDC Employees--------");
System.out.println(edcEmployees);
/*
[
Amit with 8 years of experience,
Rahul with 5 years of experience,
Jose with 15 years of experience,
Pirlo with 13 years of experience
]
*/
}
private static Predicate<Employee> seniorProfessionals(int years) {
return employee -> employee.getExperience() > years;
}
private static Predicate<Employee> edcFilter() {
return employee -> employee.getUnit() == Unit.EDC;
}
private static Predicate<Employee> javaFilter() {
return employee -> employee.getSkills().contains(Skill.JAVA);
}
public static List<Employee> getEmployeesFilteredBy(Predicate<Employee> filter) {
List<Employee> employees = initialize();
return employees.stream().filter(filter).collect(Collectors.toList());
}
//retrieve a list of employees that are sorted alphabetically by name
public static void sortByName(){
System.out.println("--------sortByName--------");
List<Employee> employees = initialize();
List<Employee> filteredEmployees = employees.stream().sorted(BY_NAME_ALPHA).collect(Collectors.toList());
System.out.println(filteredEmployees);
/*
[Amit with 8 years of experience
, Jose with 15 years of experience
, Manoj with 3 years of experience
, Peter with 7 years of experience
, Pirlo with 13 years of experience
, Rahul with 5 years of experience
, Stanley with 3 years of experience
]
*/
}
/**Sort the employees with increasing experience and if they have the same experience,
then we sort them alphabetically by name.*/
public static void sortByInreasingExpAndThenName(){
System.out.println("--------sortByInreasingExpAndThenName--------");
List<Employee> employees = initialize();
List<Employee> filteredEmployees =employees.stream().sorted(BY_ASC_EXP_THEN_NAME).collect(Collectors.toList());
System.out.println(filteredEmployees);
/*
[Manoj with 3 years of experience
, Stanley with 3 years of experience
, Rahul with 5 years of experience
, Peter with 7 years of experience
, Amit with 8 years of experience
, Pirlo with 13 years of experience
, Jose with 15 years of experience
]
*/
}
/**
* Sort the employees with decreasing experience
* and if they have the same experience, then we sort them alphabetically by name.
*/
public static void sortByDecreasingExpAndThenName(){
System.out.println("--------sortByDecreasingExpAndThenName--------");
List<Employee> employees = initialize();
List<Employee> filteredEmployees =employees.stream().sorted(BY_DESC_EXP_THEN_NAME).collect(Collectors.toList());
System.out.println(filteredEmployees);
/*
[Jose with 15 years of experience
, Pirlo with 13 years of experience
, Amit with 8 years of experience
, Peter with 7 years of experience
, Rahul with 5 years of experience
, Manoj with 3 years of experience
, Stanley with 3 years of experience
]
*/
}
/**
* Exercise
* Top 3 methods can be combined into one.
* @param sorter - Pass behavior
* @return -Sorted list.
*/
public static void getEmployees(Comparator<Employee> sorter){
System.out.println("--------getEmployees--------");
List<Employee> employees = initialize();
List<Employee> filteredEmployees =employees.stream().sorted(sorter).collect(Collectors.toList());
System.out.println(filteredEmployees);
/*
[Amit with 8 years of experience
, Jose with 15 years of experience
, Manoj with 3 years of experience
, Peter with 7 years of experience
, Pirlo with 13 years of experience
, Rahul with 5 years of experience
, Stanley with 3 years of experience
]
*/
}
public static final Comparator<Employee> BY_NAME_ALPHA =
comparing((Employee e) -> e.getName());
// Comparator for sorting employee by experience.
public static final Comparator<Employee> BY_EXPERIENCE =
comparingInt((Employee e) -> e.getExperience());
/**Sort the employees with increasing experience and if they have the same experience,
then we sort them alphabetically by name.*/
public static final Comparator<Employee> BY_ASC_EXP_THEN_NAME =
BY_EXPERIENCE.thenComparing(BY_NAME_ALPHA);
/**
* Sort the employees with decreasing experience
* and if they have the same experience, then we sort them alphabetically by name.
*/
public static final Comparator<Employee> BY_DESC_EXP_THEN_NAME =
BY_EXPERIENCE.reversed().thenComparing(BY_NAME_ALPHA);
public static List<Employee> initialize() {
List<Employee> employees = new ArrayList<>();
List<Skill> dev1Skills = new ArrayList<>();
dev1Skills.add(Skill.JAVA);
dev1Skills.add(Skill.JPA);
// Amit is a developer with Java,Jpa
employees.add(new Employee("Amit", 8, Designation.DEVELOPER, Unit.EDC, dev1Skills));
// Rahul with .NET as a programmer
List<Skill> dev2Skills = new ArrayList<>();
dev2Skills.add(Skill.MICROSOFT);
employees.add(new Employee("Rahul", 5, Designation.DEVELOPER, Unit.EDC, dev2Skills));
// Peter with python as a programmer in FS
List<Skill> dev3Skills = new ArrayList<>();
dev3Skills.add(Skill.PYTHON);
employees.add(new Employee("Peter", 7, Designation.DEVELOPER, Unit.FS, dev3Skills));
// Stanley with angular/js as a programmer in oil gas
List<Skill> dev4Skills = new ArrayList<>();
dev4Skills.add(Skill.ANGULARJS);
dev4Skills.add(Skill.JAVASCRIPT);
employees.add(new Employee("Stanley", 3, Designation.DEVELOPER, Unit.OIL_GAS, dev4Skills));
// Manoj with java and angular as a programmer in FS
List<Skill> dev5Skills = new ArrayList<>();
dev5Skills.add(Skill.ANGULARJS);
dev5Skills.add(Skill.JAVA);
employees.add(new Employee("Manoj", 3, Designation.DEVELOPER, Unit.FS, dev5Skills));
// Jose as manager
List<Skill> managerSkills = new ArrayList<>();
managerSkills.add(Skill.PMP);
employees.add(new Employee("Jose", 15, Designation.MANAGER, Unit.EDC, managerSkills));
// Pirlo as architect in edc.
List<Skill> architectSkills = new ArrayList<>();
architectSkills.add(Skill.DESIGN);
employees.add(new Employee("Pirlo", 13, Designation.ARCHITECT, Unit.EDC, architectSkills));
return employees;
}
}