Skip to content

Commit 98ac2d0

Browse files
committed
add IntStream samples
1 parent 895575c commit 98ac2d0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/com/winterbe/java8/Streams4.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.winterbe.java8;
2+
3+
import java.util.OptionalInt;
4+
import java.util.stream.IntStream;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
public class Streams4 {
10+
11+
public static void main(String[] args) {
12+
for (int i = 0; i < 10; i++) {
13+
if (i % 2 == 1) {
14+
System.out.println(i);
15+
}
16+
}
17+
18+
IntStream.range(0, 10)
19+
.forEach(i -> {
20+
if (i % 2 == 1) System.out.println(i);
21+
});
22+
23+
IntStream.range(0, 10)
24+
.filter(i -> i % 2 == 1)
25+
.forEach(System.out::println);
26+
27+
OptionalInt reduced1 =
28+
IntStream.range(0, 10)
29+
.reduce((a, b) -> a + b);
30+
System.out.println(reduced1.getAsInt());
31+
32+
int reduced2 =
33+
IntStream.range(0, 10)
34+
.reduce(7, (a, b) -> a + b);
35+
System.out.println(reduced2);
36+
}
37+
}

0 commit comments

Comments
 (0)