Java Reference
In-Depth Information
• If one class inherits the same
default
method from two interfaces, the class must override that
method; otherwise, the compiler will generate a compilation error.
• You can create your own functional interfaces by ensuring that each contains only one
abstract
method and zero or more
default
or
static
methods.
• You can declare that an interface is a functional interface by preceding it with the
@Functional-
Interface
annotation. The compiler will then ensure that the interface contains only one
abstract
method; otherwise, it'll generate a compilation error.
Self-Review Exercises
17.1
Fill in the blanks in each of the following statements:
a)
Lambda expressions implement
.
b)
Functional programs are easier to (i.e., perform multiple operations simulta-
neously) so that your programs can take advantage of multi-core architectures to en-
hance performance.
c)
With iteration the library determines how to access all the elements in a col-
lection to perform a task.
d)
The functional interface contains method
apply
that takes two
T
arguments,
performs an operation on them (such as a calculation) and returns a value of type
T
.
e)
The functional interface contains method
test
that takes a
T
argument and
returns a
boolean
, and tests whether the
T
argument satisfies a condition.
f)
A(n) represents an anonymous method—a shorthand notation for imple-
menting a functional interface.
g)
Intermediate stream operations are
—they aren't performed until a terminal
operation is invoked.
h)
The terminal stream operation
performs processing on every element in a
stream.
i)
lambdas use local variables from the enclosing lexical scope.
j)
A performance feature of lazy evaluation is the ability to perform
evaluation—
that is, to stop processing the stream pipeline as soon as the desired result is available.
k)
For
Map
s, a
BiConsumer
's first parameter represents the
and its second repre-
sents the corresponding
.
17.2
State whether each of the following is
true
or
false
. If
false
, explain why.
a) Lambda expressions can be used anywhere functional interfaces are expected.
b) Terminal operations are lazy—they perform the requested operation when they are
called.
c) Method
reduce
's first argument is formally called an identity value—a value that, when
combined with a stream element using the
IntBinaryOperator
produces the stream el-
ement's original value. For example, when summing the elements, the identity value is
1 and when getting the product of the elements the identity value is 0.
d)
Stream
method
findFirst
is a short-circuiting terminal operation that processes the
stream pipeline but terminates processing as soon as an object is found.
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.