Java Reference
In-Depth Information
Figure 5-5. Combiner
You might remember that the last step in our refactoring process, before we got to collectors,
was to put the
toString
method inline at the end of the method chain. This converted our
Figure 5-6. Finisher
Our collector's
finisher
method performs the same purpose. We've already folded our mut-
able container over a
Stream
of values, but it's not quite the final value that we want. The
finisher
gets called here, once, in order to make that conversion. This is especially useful if
we want to create an immutable final value, such as a
String
, but our container is mutable.
In order to implement the
finisher
for this operation, we just delegate to the
toString
method that we've already written (
Example 5-29
)
.
Example 5-29. A finisher produces the final value returned by the collect operation