17.6.2 Filtering Employees with Salaries in a Specified Range
Figure 17.11 demonstrates filtering Employee
s with an object that implements the functional interface Predicate<Employee>
, which is defined with a lambda in lines 34–35. Defining lambdas in this manner enables you to reuse them multiple times, as we do in lines 42 and 49. Lines 41–44 output the Employee
s with salaries in the range 4000–6000 sorted by salary as follows:
• Line 41 creates a Stream<Employee>
from the List<Employee>
.
• Line 42 filters the stream using the Predicate
named fourToSixThousand
.
• Line 43 sorts by salary the Employees
that remain in the stream. To specify a Comparator
for salaries, we use the Comparator
interface’s static
method comparing
. The method reference ...
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.