Java Reference
In-Depth Information
Let's step through this in diagram form while we're walking through the code so that we can
see how things fit together. Because collectors can be collected in parallel, we will show a
collecting operation where two container objects (e.g., StringCombiner s) are used in paral-
lel.
Each of the four components of our Collector are functions, so we'll represent them as ar-
rows. The values in our Stream are circles, and the final value we're producing will be an
oval. At the start of the collect operation our supplier is used to create new container objects
(see Figure 5-3 ).
Figure 5-3. Supplier
Our collector's accumulator performs the same job as the second argument to reduce . It
takes the current element and the result of the preceding operation and returns a new value.
We've already implemented this logic in the add method of our StringCombiner , so we just
refer to that (see Example 5-27 ) .
Example 5-27. An accumulator is a function to fold the current element into the collector
public
public BiConsumer < StringCombiner , String > accumulator () {
return
return StringCombiner: : add ;
}
Our accumulator is used to fold the stream's values into the container objects ( Figure 5-4 ) .
Search WWH ::




Custom Search