Java Reference
In-Depth Information
Figure 3-2. Internal iteration
NOTE
A
Stream
is a tool for building up complex operations on collections using a functional
approach.
We can actually break this example into two simpler operations:
▪ Finding all the artists from London
▪ Counting a list of artists
Both of these operations correspond to a method on the
Stream
interface. In order to find
artists from London, we
filter
the
Stream
. Filtering in this case means “keep only objects
that pass a test.” The test is defined by a function, which returns either
true
or
false
de-
pending on whether the artist is from London. Because we're practicing functional program-
ming when using the Streams API, we aren't changing the contents of the
Collection
;
we're just declaring what the contents of the
Stream
will be. The
count()
method counts
how many objects are in a given
Stream
.
What's Actually Going On
When I wrote the previous example, I broke it up into two simpler operations: filtering and
counting. You may think that this is pretty wasteful—when I wrote the
for
loop in