Skip to content

Commit 3ea8d22

Browse files
committed
added for reduce()
1 parent 7cd3909 commit 3ea8d22

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Modern-Java-Examples/src/com/learn/streams/StreamsReduceExample.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.learn.streams;
22

3+
import com.learn.data.Student;
4+
import com.learn.data.StudentDataBase;
5+
36
import java.util.ArrayList;
47
import java.util.List;
58
import java.util.Optional;
@@ -18,6 +21,11 @@ public static void main(String[] args) {
1821
Optional<Integer> result2 = perfromMultiplicationWithoutIdentity(new ArrayList<>());
1922
System.out.println(result2.isPresent());
2023

24+
Optional<Student> highestGpaStudent = getHightestGPAStudent();
25+
if (highestGpaStudent.isPresent()) {
26+
System.out.println(highestGpaStudent.get());
27+
}
28+
2129
}
2230

2331
/**
@@ -42,4 +50,15 @@ public static Optional<Integer> perfromMultiplicationWithoutIdentity(List<Intege
4250
return integerList.stream()
4351
.reduce((num1, num2) -> num1 * num2);
4452
}
53+
54+
/**
55+
* <p>
56+
* Find the Student with highest GPA
57+
* </p>
58+
* @return
59+
*/
60+
public static Optional<Student> getHightestGPAStudent() {
61+
return StudentDataBase.getAllStudents().stream()
62+
.reduce((student1, student2) -> student1.getGpa() > student2.getGpa() ? student1 : student2);
63+
}
4564
}

0 commit comments

Comments
 (0)