-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamApi.java
56 lines (51 loc) · 1.91 KB
/
StreamApi.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
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.function.Consumer;
public class StreamApi {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(4, 5, 7, 2, 6, 3);
// int sum = 0;
// for (int n : nums) {
// if (n % 2 == 0) {
// n = n * 2;
// sum = sum + n;
// }
// }
// System.out.println(sum);
// System.out.println(nums);
// we have stream api to do this
// Consumer<Integer> con = new Consumer<Integer>() {
// public void accept(Integer t) {
// System.out.println(t);
// }
// };
// Consumer<Integer> con = (Integer t) -> {
// System.out.println(t);
// };
// nums.forEach(con);
// that's how foreach works
// nums.forEach(n -> System.out.println(n));
Stream<Integer> s1 = nums.stream(); // returns stream of objects
// if you changes stream values // changes wil not be reflected in nums
// // s1.forEach(n -> System.out.println(n));// once you use stream we can't re
// use
// // it
// // s1.forEach(n->System.out.println(n)); // this will give you an error
// Stream<Integer> s2 = s1.filter(n -> n % 2 == 0);
// // System.out.println(s2);
// // s2.forEach(n -> System.out.println(n));
// Stream<Integer> s3 = s2.map(n -> n * 2);
// // s3.forEach(n -> System.out.println(n));
// int result = s3.reduce(0, (c, e) -> c + e);
// System.out.println(result);
// applying
int result = nums.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * 2)
.reduce(0, (c, e) -> c + e);
System.out.println(result);
}
}