-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamMapReduceExample.java
42 lines (33 loc) · 1.14 KB
/
StreamMapReduceExample.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
package com.learn.streams;
import com.learn.data.Student;
import com.learn.data.StudentDataBase;
public class StreamMapReduceExample {
public static void main(String[] args) {
System.out.println("Total Notebooks : " + totalNotebooks());
System.out.println("Total Notebooks with GradeLevel Greater than Equal to 3 : " + totalNotebooksWithFilter());
}
/**
* <p>
* Total number of notebooks that each and every student has.
* i.e. Sum of each notebooks student have.
* </p>
*/
private static int totalNotebooks() {
//Stream<Student> --> Stream<Integer>
return StudentDataBase.getAllStudents().stream()
.map(Student::getNoteBooks)
.reduce(0, Integer::sum);
}
/**
* <p>
* Total number of notebooks for student gradeLevel >= 3
* </p>
* @return
*/
private static int totalNotebooksWithFilter() {
return StudentDataBase.getAllStudents().stream()
.filter(student -> student.getGradeLevel() >= 3)
.map(Student::getNoteBooks)
.reduce(0, Integer::sum);
}
}