-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsMinByMaxByExample.java
More file actions
49 lines (42 loc) · 1.37 KB
/
StreamsMinByMaxByExample.java
File metadata and controls
49 lines (42 loc) · 1.37 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
package com.learn.streams_terminal;
import com.learn.data.Student;
import com.learn.data.StudentDataBase;
import java.util.Comparator;
import java.util.Optional;
import java.util.stream.Collectors;
public class StreamsMinByMaxByExample {
public static void main(String[] args) {
Optional<Student> minGpaStudent = minByExample();
if (minGpaStudent.isPresent()) {
System.out.println("Minimum GPA Student : " + minGpaStudent.get());
}
Optional<Student> maxGpaStudent = maxByExample();
if (maxGpaStudent.isPresent()) {
System.out.println("Maximum GPA Student : " + maxGpaStudent.get());
}
}
/**
* <p>
* minBy() takes i/p as Comparator.
* Get the Student who has the least GPA.
* </p>
* @return
*/
public static Optional<Student> minByExample() {
return StudentDataBase.getAllStudents()
.stream()
.collect(Collectors.minBy(Comparator.comparing(Student::getGpa)));
}
/**
* <p>
* maxBy() takes i/p as Comparator.
* Get the Student who has the Highest GPA.
* </p>
* @return
*/
public static Optional<Student> maxByExample() {
return StudentDataBase.getAllStudents()
.stream()
.collect(Collectors.maxBy(Comparator.comparing(Student::getGpa)));
}
}