forked from navinreddy20/Javacode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22.3 Stream API.java
More file actions
31 lines (24 loc) · 801 Bytes
/
22.3 Stream API.java
File metadata and controls
31 lines (24 loc) · 801 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
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import java.util.function.Consumer;
public class Demo {
public static void main(String[] args){
List<Integer> nums=Arrays.asList(4,5,7,3,2,6);
// Stream<Integer> s1=nums.stream();
// Stream<Integer> s2= s1.filter(n ->n%2==0);
// Stream<Integer> s3= s2.map(n->n*2);
// int result=s3.reduce(0,(c,e)->c+e);
//
// s2.forEach(n -> System.out.println(n));
// s3.forEach(n -> System.out.println(n));
//
// s1.forEach(n-> System.out.println(n));
// s1.forEach(n-> System.out.println(n));
int result=nums.stream()
.filter(n-> n%2==0)
.map(n->n*2)
.reduce(0, (c,e)-> c+e);
System.out.println(result);
}
}