-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdditionEx.java
More file actions
38 lines (25 loc) · 901 Bytes
/
Copy pathAdditionEx.java
File metadata and controls
38 lines (25 loc) · 901 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
package streamInterviewQA.numeric;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
public class AdditionEx {
public static void main(String[] args) {
List<Integer> asList = Arrays.asList(10, 5, 4, 20);
asList.stream().mapToInt(Integer::intValue).sum();
asList.stream().mapToInt(i -> i).reduce(0, (x, y) -> x + y);
asList.stream().collect(Collectors.summingInt(Integer::intValue));
asList.stream().reduce(0, (x, y) -> x + y);
asList.stream().reduce(0, Integer::sum);
}
// Alternate to this below
public Integer ex(Integer identity) {
Integer result = identity;
List<Integer> stream = new ArrayList<>();
BinaryOperator<Integer> accumulator = (x, y) -> x + y;
for (Integer element : stream)
result = accumulator.apply(result, element);
return result;
}
}