File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments