-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamAPI.java
More file actions
33 lines (23 loc) · 962 Bytes
/
StreamAPI.java
File metadata and controls
33 lines (23 loc) · 962 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
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class StreamAPI {
public static void main(String[] args) {
List<Integer> myList = Arrays.asList(4, 2, 1, 5, 7, 8, 12, 22, 10, 13);
Stream<Integer> stream = myList.stream();
// Functional interface that specifies filter condition
Predicate<Integer> predicate = (n) -> n%2 == 0? true : false;
// Specifies the function to be applied
Function<Integer, Integer> function = n -> n*2;
//
Consumer<Integer> consumer = (n) -> System.out.println(n);
stream.filter(predicate)
.map(function)
.forEach(consumer);
// myList.forEach(n -> System.out.println(n));
// stream.forEach(n -> System.out.println(n));
}
}