-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamApi.java
More file actions
30 lines (20 loc) · 817 Bytes
/
Copy pathStreamApi.java
File metadata and controls
30 lines (20 loc) · 817 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
package Java8;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class StreamApi {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<>();
for(int i=0; i<100; i++) myList.add(i);
//sequential stream
Stream<Integer> sequentialStream = myList.stream();
//parallel stream
Stream<Integer> parallelStream = myList.parallelStream();
//using lambda with Stream API, filter example
Stream<Integer> highNums = parallelStream.filter(p -> p > 85);
//using lambda in forEach
highNums.forEach(p -> System.out.println("High Nums parallel="+p));
Stream<Integer> highNumsSeq = sequentialStream.filter(p -> p > 85);
highNumsSeq.forEach(p -> System.out.println("High Nums sequential="+p));
}
}