Java Reference
In-Depth Information
17.3
Write a lambda or method reference for each of the following tasks:
a) Write a lambda that can be used in place of the following anonymous inner class:
new IntConsumer()
{
public void accept( int value)
{
System.out.printf( "%d " , value);
}
}
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.
Answers to Self-Review Exercises
17.1 a) functional interfaces. b) parallelize. c) internal. d) BinaryOperator<T> . e) Predicate<T> .
f ) lambda expression. g) lazy. h) forEach . i) Capturing. j) short circuit. k) key, value.
17.2 a) True. b) False. Terminal operations are eager —they perform the requested operation
when they are called. c) False. When summing the elements, the identity value is 0 and when getting
the product of the elements the identity value is 1. d) True. e) False. Stream method flatMap re-
ceives a Function that maps an object into a stream. f) False. Should say: “…does not override them,
…” instead of “overrides them.”
17.3
a) value -> System.out.printf( "%d " , value)
b) String::toUpperCase
c) () -> "Welcome to lambdas!"
d) Math::sqrt
e) value -> value * value * value
Exercises
17.4
Fill in the blanks in each of the following statements:
a)
Stream
are formed from stream sources, intermediate operations and terminal
operations.
b)
The following code uses the technique of
iteration:
int sum = 0 ;
for ( int counter = 0 ; counter < values.length; counter++)
sum += values[counter];
c)
Functional programming capabilities focus on
—not modifying the data
source being processed or any other program state.
d)
The functional interface contains method accept that takes a T argument and
returns void ; accept performs a task with its T argument, such as outputting the object,
invoking a method of the object, etc.
e)
The functional interface contains method get that takes no arguments and
produces a value of type T —this is often used to create a collection object in which a
stream operation's results are placed.
 
Search WWH ::




Custom Search