Java Reference
In-Depth Information
Finally, all the partial results are combined pairwise using the function returned by the combiner
method of the collector. This is done by combining results corresponding to substreams associated
with each split of the original stream.
Characteristics method
The last method, characteristics, returns an immutable set of Characteristics, defining the
behavior of the collector—in particular providing hints about whether the stream can be reduced
in parallel and which optimizations are valid when doing so. Characteristics is an enumeration
containing three items:
UNORDERED The result of the reduction isn't affected by the order in which the items in the
stream are traversed and accumulated.
CONCURRENT The accumulator function can be called concurrently from multiple threads, and
then this collector can perform a parallel reduction of the stream. If the collector isn't also flagged as
UNORDERED , it can perform a parallel reduction only when it's applied to an unordered data
source.
IDENTITY_FINISH This indicates the function returned by the finisher method is the identity
one, and its application can be omitted. In this case, the accumulator object is directly used as the final
result of the reduction process. This also implies that it's safe to do an unchecked cast from the
accumulator A to the result R .
The ToListCollector developed so far is IDENTITY_FINISH, because the List used to
accumulate the elements in the stream is already the expected final result and doesn't need any
further transformation, but it isn't UNORDERED because if you apply it to an ordered stream
you want this ordering to be preserved in the resulting List. Finally, it's CONCURRENT, but
following what we just said, the stream will be processed in parallel only if its underlying data
source is unordered.
6.5.2. Putting them all together
The five methods analyzed in the preceding subsection are everything you need to develop your
own ToListCollector, so you can implement it by putting all of them together, as the next listing
shows.
 
Search WWH ::




Custom Search