July 2023
Intermediate to advanced
276 pages
6h 55m
English
By now I’m sure you’ve recognized that Collectors, with so many functions, may be one of the most comprehensive utility classes in the JDK. If you’re looking for an intellectual company to hang out with on a Friday evening, I suggest you invite the Collectors—you won’t be disappointed. In addition to all the methods we’ve seen, the Collectors utility class also has a function to sum values. Let’s use that to total the number of email addresses a person has, while grouping the persons in a collection based on their last name.
| | var namesAndEmailAddressesCount = |
| | SAMPLE_DATA.stream() |
| | .collect(groupingBy( |
| | Person::lastName, |
| | summingInt(person -> person.emailAddresses().size()))); |
| | |
| | System.out.println(namesAndEmailAddressesCount); ... |
Read now
Unlock full access