Java Reference
In-Depth Information
37
38
// product of values with reduce method
39
System.out.printf( "Product via reduce method: %d%n" ,
40
IntStream.of(values)
.reduce( 1 , (x, y) -> x * y)
41
);
42
43
// even values displayed in sorted order
44
System.out.printf( "%nEven values displayed in sorted order: " );
45
IntStream.of(values)
.filter(value -> value % 2 == 0 )
.sorted()
.forEach(value -> System.out.printf( "%d " , value));
46
47
48
49
System.out.println();
50
51
// odd values multiplied by 10 and displayed in sorted order
52
System.out.printf(
53
"Odd values multiplied by 10 displayed in sorted order: " );
54
IntStream.of(values)
.filter(value -> value % 2 != 0 )
.map(value -> value * 10 )
.sorted()
.forEach(value -> System.out.printf( "%d " , value));
55
56
57
58
59
System.out.println();
60
61
// sum range of integers from 1 to 10, exlusive
62
System.out.printf( "%nSum of integers from 1 to 9: %d%n" ,
63
IntStream.range( 1 , 10 ).sum()
);
64
65
// sum range of integers from 1 to 10, inclusive
66
System.out.printf( "Sum of integers from 1 to 10: %d%n" ,
67
IntStream.rangeClosed( 1 , 10 ).sum()
);
68
}
69
} // end class IntStreamOperations
Original values: 3 10 6 1 4 8 2 5 9 7
Count: 10
Min: 1
Max: 10
Sum: 55
Average: 5.50
Sum via reduce method: 55
Sum of squares via reduce method: 385
Product via reduce method: 3628800
Even values displayed in sorted order: 2 4 6 8 10
Odd values multiplied by 10 displayed in sorted order: 10 30 50 70 90
Sum of integers from 1 to 9: 45
Sum of integers from 1 to 10: 55
Fig. 17.5 | Demonstrating IntStream operations. (Part 2 of 2.)
Search WWH ::




Custom Search