17.6.4 Mapping Employees to Unique Last Name Strings
You previously used map
operations to perform calculations on int
values and to convert String
s to uppercase letters. In both cases, the resulting streams contained values of the same types as the original streams. Figure 17.13 shows how to map objects of one type (Employee
) to objects of a different type (String
). Lines 77–81 perform the following tasks:
• Line 77 creates a Stream<Employee>
.
• Line 78 maps the Employees
to their last names using the instance method reference Employee::getName
as method map
’s Function
argument. The result is a Stream<String>
.
• Line 79 calls Stream
method distinct on the Stream<String>
to eliminate any duplicate String
objects in a Stream<String>
.
• Line 80 ...
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.