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
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.winterbe.java8;
2+
3+
importjava.util.OptionalInt;
4+
importjava.util.stream.IntStream;
5+
6+
/**
7+
* @author Benjamin Winterberg
8+
*/
9+
publicclassStreams4{
10+
11+
publicstaticvoidmain(String[] args){
12+
for (inti = 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+
OptionalIntreduced1 =
28+
IntStream.range(0, 10)
29+
.reduce((a, b) -> a + b);
30+
System.out.println(reduced1.getAsInt());
31+
32+
intreduced2 =
33+
IntStream.range(0, 10)
34+
.reduce(7, (a, b) -> a + b);
35+
System.out.println(reduced2);
36+
}
37+
}

0 commit comments

Comments
(0)