Java Reference
In-Depth Information
lambda expressions, they still need to be
effectively
final
.
Whenever you pass lambda expressions into the higher-order functions on the
Stream
inter-
face, you should seek to avoid side effects. The only exception to this is the
forEach
meth-
od, which is a terminal operation.
Key Points
▪ Internal iteration is a way of iterating over a collection that delegates more control over
the iteration to the collection.
▪ A
Stream
is the internal iteration analogue of an
Iterator
.
▪ Many common operations on collections can be performed by combining methods on
Stream
with lambda expressions.
Exercises
NOTE
You can find the answers to these exercises on
GitHub
.
1.
Common Stream operations
. Implement the following:
a. A function that adds up numbers, i.e.,
int addUp(Stream<Integer> num-
bers)
b. A function that takes in artists and returns a list of strings with their names and
places of origin
c. A function that takes in albums and returns a list of albums with at most three
tracks
2.
Iteration
. Convert this code sample from using external iteration to internal iteration: