-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamsReduceExample.java
64 lines (50 loc) · 2 KB
/
StreamsReduceExample.java
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
package com.learn.streams;
import com.learn.data.Student;
import com.learn.data.StudentDataBase;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class StreamsReduceExample {
public static void main(String[] args) {
List<Integer> numbers = List.of(10, 20, 30, 40);
System.out.println(performMultiplication(numbers));
Optional<Integer> result1 = perfromMultiplicationWithoutIdentity(numbers);
System.out.println(result1.isPresent());
System.out.println(result1.get());
Optional<Integer> result2 = perfromMultiplicationWithoutIdentity(new ArrayList<>());
System.out.println(result2.isPresent());
Optional<Student> highestGpaStudent = getHightestGPAStudent();
if (highestGpaStudent.isPresent()) {
System.out.println(highestGpaStudent.get());
}
}
/**
* <p>
* Preform multiplication of the list.
* </p>
* @param integerList
* @return
*/
public static int performMultiplication(List<Integer> integerList) {
return integerList.stream()
// num1 = 1 num2 = 10 (from Stream) => 1 * 10 result returned
// num1 = 10, num2 = 20 (from Stream) => 10 * 20 result returned
// num1 = 200 , num2 = 30 (from Stream) => 200 * 30 result returned
// num1 = 6000 , num2 = 40 (from Stream) => 24000 result returned
.reduce(1, (num1 , num2) -> num1 * num2);
}
public static Optional<Integer> perfromMultiplicationWithoutIdentity(List<Integer> integerList) {
return integerList.stream()
.reduce((num1, num2) -> num1 * num2);
}
/**
* <p>
* Find the Student with highest GPA
* </p>
* @return
*/
public static Optional<Student> getHightestGPAStudent() {
return StudentDataBase.getAllStudents().stream()
.reduce((student1, student2) -> student1.getGpa() > student2.getGpa() ? student1 : student2);
}
}