... the elements that are divisible by 2 (that is, the even integers), producing a stream of the even integers from 2, 4, 6, 8 and 10.
-
Line 12 maps each element (
x) in the stream to that element times 3, producing a stream of the even integers from 6, 12, 18, 24 and 30. -
Line 13 reduces the stream to the sum of its elements (90).
The new feature here is the filtering operation in line 11. IntStream method filter receives as its argument a method that takes one parameter and returns a boolean result. If the result is true for a given element, that element is included in the resulting stream.
The lambda in line 11:
x -> x % 2 == 0
determines whether its int argument is divisible by 2 (that is, the remainder after dividing by 2 is 0) and, if so, returns ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access