-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReductionExample.java
More file actions
42 lines (36 loc) ยท 964 Bytes
/
ReductionExample.java
File metadata and controls
42 lines (36 loc) ยท 964 Bytes
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 ch17.sec11;
import java.util.Arrays;
import java.util.List;
public class ReductionExample {
public static void main(String[] args) {
List<Student> list = Arrays.asList(
new Student("ํ", 92),
new Student("์ ", 95),
new Student("๊น", 88)
);
// sum
int sum = list.stream()
.mapToInt(Student::getScore)
.sum();
System.out.println("sum = " + sum);
// reduce
int reduce = list.stream()
.map(Student::getScore)
.reduce(0, (x, y) -> x + y);
System.out.println("reduce = " + reduce);
}
}
class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
}