Java Reference
In-Depth Information
But, for reasons centered on better exploiting parallelism, the designers didn't do this. Java 8
instead contains a whole new Collections-like API called Streams, containing a comprehensive
set of operations similar to filter that functional programmers may be familiar with (for example,
map, reduce), along with methods to convert between Collections and Streams, which we now
investigate.
1.3. Streams
Nearly every Java application makes and processes collections. But working with collections
isn't always ideal. For example, let's say you need to filter expensive transactions from a list and
then group them by currency. You'd need to write a lot of boilerplate code to implement this
data processing query, as shown here:
In addition, it's difficult to understand at a glance what the code does because of the multiple
nested control-flow statements.
Using the Streams API, you can solve this problem as follows:
Don't worry about this code for now because it may look like a bit of magic. Chapters 4 - 7 are
dedicated to explaining how to make sense of the Streams API. For now it's worth noticing that
the Streams API provides a very different way to process data in comparison to the Collections
API. Using a collection, you're managing the iteration process yourself. You need to iterate
through each element one by one using a for-each loop and then process the elements. We call
this way of iterating over data external iteration . In contrast, using the Streams API, you don't
 
Search WWH ::




Custom Search