3Chapter 17 Lambdas and Streams
e) Stream method flatMap receives a Function that maps a stream into an object. For ex-
ample, the object could be a String containing words and the result could be another
intermediate Stream<String> for the individual words.
f) When a class implements an interface with default methods and overrides them, the
class inherits the default methods’ implementations. An interface’s designer can now
evolve an interface by adding new default and static methods without breaking ex-
isting code that implements the interface.
17.3 Write a lambda or method reference for each of the following tasks:
a) Write a lambda that can that can be passed to a method with an IntConsumer parameter.
The lambda should display its argument followed by a space.
b) Write a method reference that can be used in place of the following lambda:
(String s) -> {return s.toUpperCase();}
c) Write a no-argument lambda that implicitly returns the String “Welcome to lambdas!”.
d) Write a method reference for Math method sqrt.
e) Create a one-parameter lambda that returns the cube of its argument.
Exercises
NOTE: Solutions to the programming exercises are located in the ch12solutions folder.
Each exercise has its own folder named ex17_## where ## is a two-digit number representing
the exercise number. For example, Exercise 17.10’s solution is located in the folder ex17_10.
17.4 Fill in the blanks in each of the following statements:
a) Stream are formed from stream sources, intermediate operations and termi-
nal operations.
b) The following code uses the technique of iteration:
c) Functional programming capabilities focus on —not modifying the data
source being processed or any other program state.
d) An implementation of the functional interface takes a T argument and returns
void, and performs a task with its T argument, such as outputting the object, invoking
a method of the object, etc.
1int sum = 0;
2
3for (int counter = 0; counter < values.length; counter++) {
4 sum += values[counter];
5}
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.