Java Reference
In-Depth Information
Figure C.2. The StreamForker building blocks
In the figure, the StreamForker in the upper left has a Map, where each operation to be
performed on the stream, defined by a function, is indexed by a key. The
ForkingStreamConsumer on the right holds a queue for each of these operations and consumes
all the elements in the original stream, multiplexing them to all the queues.
At the bottom of the figure, each queue has a BlockingQueueSpliterator pulling its items and
acting as a source for a different stream. Finally, each of these streams, forked by the original
one, is passed as argument to one of the functions, thus executing one of the operations to be
performed. You now have all the components of your StreamForker, so it's ready to use.
C.1.3. Putting the StreamForker to work
Let's put the StreamForker to work on the menu data model that we defined in chapter 4 , by
forking the original stream of dishes to perform four different operations in parallel on it, as
shown in the next listing. In particular, you want to generate a comma-separated list of the
names of all available dishes, calculate the total calories of the menu, find the dish with the most
calories, and group all dishes by their type.
Listing C.6. Putting the StreamForker to work
Stream<Dish> menuStream = menu.stream();
 
Search WWH ::




Custom Search