Java Reference
In-Depth Information
Dishes by type: {OTHER=[french fries, rice, season fruit, pizza], MEAT=[pork, beef, chicken],
FISH=[prawns, salmon]}
C.2. Performance considerations
For performance reasons you shouldn't take for granted that this approach is more efficient than
traversing the stream several times. The overhead caused by the use of the blocking queues can
easily outweigh the advantages of executing the different operations in parallel when the stream
is made of data that's all in memory.
Conversely, accessing the stream only once could be a winning choice when this involves some
expensive I/O operations, such as when the source of the stream is a huge file; so (as usual) the
only meaningful rule when optimizing the performance of your application is to “Just measure
it!”
This example demonstrates how it can be possible to execute multiple operations on the same
stream in one shot. More importantly, we believe this proves that even when a specific feature
isn't provided by the native Java API, the flexibility of lambda expressions and a bit of creativity
in reusing and combining what's already available can let you implement the missing feature on
your own.
 
Search WWH ::




Custom Search