17.6.7 Summing and Averaging Employee Salaries
Figure 17.16 demonstrates Stream
method mapToDouble (lines 119, 126 and 132), which maps objects to double
values and returns a DoubleStream
. In this case, we map Employee
objects to their salaries so that we can calculate the sum and average. Method mapToDouble
receives an object that implements the functional interface ToDoubleFunction (package java.util.function
). This interface’s applyAsDouble method invokes an instance method on an object and returns a double
value. Lines 119, 126 and 132 each pass to mapToDouble
the Employee
instance method reference Employee::getSalary
, which returns the current Employee
’s salary as a double
. The compiler converts this method reference into an object that ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.