Java Reference
In-Depth Information
specialized implementation of
reduce
. For example, line 31 shows how to sum an
Int-
Steam
's values using
reduce
, rather than
sum
. The first argument (
0
) is a value that helps
you begin the reduction operation and the second argument is an object that implements
the
IntBinaryOperator
functional interface (package
java.util.function
). The lambda:
(x, y) -> x + y
implements the interface's
applyAsInt
method, which receives two
int
values (represent-
ing the left and right operands of a binary operator) and performs a calculation with the
values—in this case, adding the values. A lambda with two or more parameters
must
en-
close them in parentheses. Evaluation of the reduction proceeds as follows:
• On the first call to
reduce
, lambda parameter
x
's value is the identity value (
0
)
and lambda parameter
y
's value is the
first
int
in the stream (
3
), producing the
sum
3
(0 + 3).
• On the next call to
reduce
, lambda parameter
x
's value is the result of the first
calculation (
3
) and lambda parameter
y
's value is the
second
int
in the stream
(
10
), producing the sum
13
(3 + 10).
• On the next call to
reduce
, lambda parameter
x
's value is the result of the previ-
ous calculation (
13
) and lambda parameter
y
's value is the
third
int
in the stream
(
6
), producing the sum
19
(13 + 6).
This process continues producing a running total of the
IntSteam
's values until they've all
been used, at which point the final sum is returned.
Method
reduce
's Identity Value Argument
Method
reduce
's first argument is formally called an
identity value
—a value that, when
combined with any stream element using the
IntBinaryOperator
produces that element's
original value. For example, when summing the elements, the identity value is 0 (any
int
value added to 0 results in the original value) and when getting the product of the elements
the identity value is 1 (any
int
value multiplied by 1 results in the original value).
Summing the Squares of the Values with Method
reduce
Lines 34-36 of Fig. 17.5 use method
reduce
to calculate the sums of the squares of the
IntSteam
's values. The lambda in this case, adds the
square
of the current value to the run-
ning total. Evaluation of the reduction proceeds as follows:
• On the first call to
reduce
, lambda parameter
x
's value is the identity value (
0
)
and lambda parameter
y
's value is the
first
int
in the stream (
3
), producing the
value
9
(0 + 3
2
).
• On the next call to
reduce
, lambda parameter
x
's value is the result of the first
calculation (
9
) and lambda parameter
y
's value is the
second
int
in the stream
(
10
), producing the sum
109
(9 + 10
2
).
• On the next call to
reduce
, lambda parameter
x
's value is the result of the previ-
ous calculation (
109
) and lambda parameter
y
's value is the
third
int
in the
stream (
6
), producing the sum
145
(109 + 6
2
).
This process continues producing a running total of the squares of the
IntSteam
's values
until they've all been used, at which point the final sum is returned.