-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamsLimitSkipExample.java
39 lines (29 loc) · 1.09 KB
/
StreamsLimitSkipExample.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
package com.learn.streams;
import java.util.List;
import java.util.Optional;
public class StreamsLimitSkipExample {
public static void main(String[] args) {
List<Integer> integerList = List.of(10, 20, 30, 40, 50);
Optional<Integer> sum = sumWithLimit(integerList);
if (sum.isPresent()) {
System.out.println("Limit Sum : " + sum.get());
}
Optional<Integer> sumWithSkip = sumWithSkip(integerList);
if (sumWithSkip.isPresent()) {
System.out.println("Skip Sum : " + sumWithSkip.get());
}
}
public static Optional<Integer> sumWithLimit(List<Integer> integerList) {
return integerList.stream()
.limit(2)
// only 10 and 20 will be passed to reduce function
.reduce(Integer::sum);
}
public static Optional<Integer> sumWithSkip(List<Integer> integerList) {
return integerList.stream()
// first 3 numbers are skipped
.skip(3)
//it passes only 40 and 50.
.reduce(Integer::sum);
}
}