February 2014
Beginner
1248 pages
62h 25m
English
Figure 17.12 shows how to use streams to sort objects by multiple fields. In this example, we sort Employees by last name, then, for Employees with the same last name, we also sort them by first name. To do so, we begin by creating two Functions that each receive an Employee and return a String:
• byFirstName (line 54) is assigned a method reference for Employee instance method getFirstName
• byLastName (line 55) is assigned a method reference for Employee instance method getLastName
Next, we use these Functions to create a Comparator (lastThenFirst; lines 58–59) that first compares two Employees by last name, then compares them by first name. We use Comparator method comparing to create a Comparator ...
Read now
Unlock full access