Java Reference
In-Depth Information
Figure 3-1. External iteration
External iteration has some negative issues associated with it, too. First, it becomes hard to
abstract away the different behavioral operations that we'll encounter later in this chapter. It
is also an approach that is inherently serial in nature. The big-picture issue here is that using
a for loop conflates what you are doing with how you are doing it.
An alternative approach, internal iteration , is shown in Example 3-3 . The first thing to notice
is the call to stream() , which performs a similar role to the call to iterator() in the previ-
ous example. Instead of returning an Iterator to control the iteration, it returns the equival-
ent interface in the internal iteration world: Stream .
Example 3-3. Counting London-based artists using internal iteration
long
long count = allArtists . stream ()
. filter ( artist -> artist . isFrom ( "London" ))
. count ();
Figure 3-2 depicts the flow of method calls with respect to the library; compare it with Fig-
ure 3-1 .
Search WWH ::




Custom Search