-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFP04CustomClass.java
More file actions
262 lines (202 loc) · 8.07 KB
/
FP04CustomClass.java
File metadata and controls
262 lines (202 loc) · 8.07 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
package programming;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
class Course {
private String name;
private String category;
private int reviewScore;
private int noOfStudents;
public Course(String name, String category, int reviewScore, int noOfStudents) {
super();
this.name = name;
this.category = category;
this.reviewScore = reviewScore;
this.noOfStudents = noOfStudents;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getReviewScore() {
return reviewScore;
}
public void setReviewScore(int reviewScore) {
this.reviewScore = reviewScore;
}
public int getNoOfStudents() {
return noOfStudents;
}
public void setNoOfStudents(int noOfStudents) {
this.noOfStudents = noOfStudents;
}
public String toString() {
return name + ":" + noOfStudents + ":" + reviewScore;
}
}
public class FP04CustomClass {
public static void main(String[] args) {
List<Course> courses = List.of(new Course("Spring", "Framework", 98, 20000),
new Course("Spring Boot", "Framework", 95, 18000), new Course("API", "Microservices", 97, 22000),
new Course("Microservices", "Microservices", 96, 25000),
new Course("FullStack", "FullStack", 91, 14000), new Course("AWS", "Cloud", 92, 21000),
new Course("Azure", "Cloud", 99, 21000), new Course("Docker", "Cloud", 92, 20000),
new Course("Kubernetes", "Cloud", 91, 20000));
// allMatch, noneMatch, anyMatch
Predicate<Course> reviewScoreGreaterThan95Predicate
= course -> course.getReviewScore() > 95;
Predicate<Course> reviewScoreGreaterThan90Predicate
= course -> course.getReviewScore() > 90;
Predicate<Course> reviewScoreLessThan90Predicate
= course -> course.getReviewScore() < 90;
System.out.println(courses.stream().allMatch(reviewScoreGreaterThan95Predicate));
System.out.println(courses.stream().noneMatch(reviewScoreLessThan90Predicate));
System.out.println(courses.stream().anyMatch(reviewScoreLessThan90Predicate));
System.out.println(courses.stream().anyMatch(reviewScoreGreaterThan95Predicate));
Comparator<Course> comparingByNoOfStudentsIncreasing
= Comparator.comparingInt(Course::getNoOfStudents);
System.out.println(
courses.stream()
.sorted(comparingByNoOfStudentsIncreasing)
.collect(Collectors.toList()));
//[FullStack:14000:91, Spring Boot:18000:95, Spring:20000:98, Docker:20000:92, Kubernetes:20000:91, AWS:21000:92, Azure:21000:99, API:22000:97, Microservices:25000:96]
Comparator<Course> comparingByNoOfStudentsDecreasing
= Comparator.comparingInt(Course::getNoOfStudents).reversed();
System.out.println(
courses.stream()
.sorted(comparingByNoOfStudentsDecreasing)
.collect(Collectors.toList()));
//[Microservices:25000:96, API:22000:97, AWS:21000:92, Azure:21000:99, Spring:20000:98, Docker:20000:92, Kubernetes:20000:91, Spring Boot:18000:95, FullStack:14000:91]
Comparator<Course> comparingByNoOfStudentsAndNoOfReviews
= Comparator.comparingInt(Course::getNoOfStudents)
.thenComparingInt(Course::getReviewScore)
.reversed();
System.out.println(
courses.stream()
.sorted(comparingByNoOfStudentsAndNoOfReviews)
.collect(Collectors.toList()));
//[Microservices:25000:96, API:22000:97, Azure:21000:99, AWS:21000:92, Spring:20000:98, Docker:20000:92, Kubernetes:20000:91, Spring Boot:18000:95, FullStack:14000:91]
System.out.println(
courses.stream()
.sorted(comparingByNoOfStudentsAndNoOfReviews)
.limit(5)
.collect(Collectors.toList()));
//[Microservices:25000:96, API:22000:97, Azure:21000:99, AWS:21000:92, Spring:20000:98]
System.out.println(
courses.stream()
.sorted(comparingByNoOfStudentsAndNoOfReviews)
.skip(3)
.collect(Collectors.toList()));
//[AWS:21000:92, Spring:20000:98, Docker:20000:92, Kubernetes:20000:91, Spring Boot:18000:95, FullStack:14000:91]
System.out.println(
courses.stream()
.sorted(comparingByNoOfStudentsAndNoOfReviews)
.skip(3)
.limit(5)
.collect(Collectors.toList()));
//[AWS:21000:92, Spring:20000:98, Docker:20000:92, Kubernetes:20000:91, Spring Boot:18000:95]
System.out.println(courses);
//[Spring:20000:98, Spring Boot:18000:95, API:22000:97, Microservices:25000:96, FullStack:14000:91, AWS:21000:92, Azure:21000:99, Docker:20000:92, Kubernetes:20000:91]
System.out.println(
courses.stream()
.takeWhile(course -> course.getReviewScore()>=95)
.collect(Collectors.toList()));
//[Spring:20000:98, Spring Boot:18000:95, API:22000:97, Microservices:25000:96]
System.out.println(
courses.stream()
.dropWhile(course -> course.getReviewScore()>=95)
.collect(Collectors.toList()));
//[FullStack:14000:91, AWS:21000:92, Azure:21000:99, Docker:20000:92, Kubernetes:20000:91]
System.out.println(
courses.stream()
.max(comparingByNoOfStudentsAndNoOfReviews));
//Optional[FullStack:14000:91]
System.out.println(
courses.stream()
.min(comparingByNoOfStudentsAndNoOfReviews)
.orElse(new Course("Kubernetes", "Cloud", 91, 20000))
);
//Optional[Microservices:25000:96]
//Microservices:25000:96
System.out.println(
courses.stream()
.filter(reviewScoreLessThan90Predicate)
.min(comparingByNoOfStudentsAndNoOfReviews)
.orElse(new Course("Kubernetes", "Cloud", 91, 20000))
);
//Optional.empty
//Kubernetes:20000:91
System.out.println(
courses.stream()
.filter(reviewScoreLessThan90Predicate)
.findFirst()
);//Optional.empty
System.out.println(
courses.stream()
.filter(reviewScoreGreaterThan95Predicate)
.findFirst()
);//Optional[Spring:20000:98]
System.out.println(
courses.stream()
.filter(reviewScoreGreaterThan95Predicate)
.findAny()
);//Optional[Spring:20000:98]
System.out.println(
courses.stream()
.filter(reviewScoreGreaterThan95Predicate)
.mapToInt(Course::getNoOfStudents)
.sum());//88000
System.out.println(
courses.stream()
.filter(reviewScoreGreaterThan95Predicate)
.mapToInt(Course::getNoOfStudents)
.average());//OptionalDouble[22000.0]
System.out.println(
courses.stream()
.filter(reviewScoreGreaterThan95Predicate)
.mapToInt(Course::getNoOfStudents)
.count());//4
System.out.println(
courses.stream()
.filter(reviewScoreGreaterThan95Predicate)
.mapToInt(Course::getNoOfStudents)
.max());//OptionalInt[25000]
System.out.println(
courses.stream()
.collect(Collectors.groupingBy(Course::getCategory)));
//{Cloud=[AWS:21000:92, Azure:21000:99, Docker:20000:92, Kubernetes:20000:91],
// FullStack=[FullStack:14000:91],
// Microservices=[API:22000:97, Microservices:25000:96],
// Framework=[Spring:20000:98, Spring Boot:18000:95]}
System.out.println(
courses.stream()
.collect(Collectors.groupingBy(Course::getCategory, Collectors.counting())));
//{Cloud=4, FullStack=1, Microservices=2, Framework=2}
System.out.println(
courses.stream()
.collect(Collectors.groupingBy(Course::getCategory,
Collectors.maxBy(Comparator.comparing(Course::getReviewScore)))));
//{Cloud=Optional[Azure:21000:99], FullStack=Optional[FullStack:14000:91], Microservices=Optional[API:22000:97], Framework=Optional[Spring:20000:98]}
System.out.println(
courses.stream()
.collect(Collectors.groupingBy(Course::getCategory,
Collectors.mapping(Course::getName, Collectors.toList()))));
//{Cloud=[AWS, Azure, Docker, Kubernetes], FullStack=[FullStack], Microservices=[API, Microservices], Framework=[Spring, Spring Boot]}
Predicate<Course> reviewScoreGreaterThan95Predicate2
= createPredicateWithCutoffReviewScore(95);
Predicate<Course> reviewScoreGreaterThan90Predicate2
= createPredicateWithCutoffReviewScore(90);
}
private static Predicate<Course> createPredicateWithCutoffReviewScore(int cutoffReviewScore) {
return course -> course.getReviewScore() > cutoffReviewScore;
}
}