Java Reference
In-Depth Information
Table 4.2. Terminal operations
Operation Type
Purpose
forEach
Terminal Consumes each element from a stream and applies a lambda to each of them.
The operation returns void.
count
Terminal Returns the number of elements in a stream. The operation returns a long.
collect
Terminal Reduces the stream to create a collection such as a List, a Map, or even an
Integer. See chapter 6 for more detail.
In the next chapter, we detail the available stream operations with use cases so you can see what
kinds of queries you can express with them. We look at many patterns such as filtering, slicing,
finding, matching, mapping, and reducing, which can be used to express sophisticated data
processing queries.
Because chapter 6 deals with collectors in great detail, the only use this chapter and the next one
make of the collect() terminal operation on streams is the special case of collect(toList()), which
creates a List whose elements are the same as those of the stream it's applied to.
4.5. Summary
Here are some key concepts to take away from this chapter:
A stream is a sequence of elements from a source that supports data processing operations.
Streams make use of internal iteration: the iteration is abstracted away through operations such as
filter , map , and sorted .
There are two types of stream operations: intermediate and terminal operations.
Intermediate operations such as filter and map return a stream and can be chained together. They're
used to set up a pipeline of operations but don't produce any result.
Terminal operations such as forEach and count return a nonstream value and process a stream
pipeline to return a result.
The elements of a stream are computed on demand.
 
Search WWH ::




Custom Search