Java Reference
In-Depth Information
Key Points
▪ A lambda expression is a method without a name that is used to pass around behavior as
if it were data.
▪ Lambda expressions look like this: BinaryOperator<Integer> add = (x, y) → x +
y .
▪ A functional interface is an interface with a single abstract method that is used as the
type of a lambda expression.
Exercises
At the end of each chapter is a series of exercises to give you an opportunity to practice what
you've learned during the chapter and help you learn the new concepts. The answers to these
exercises can be found on GitHub .
1. Questions about the Function functional interface ( Example 2-15 ).
Example 2-15. The function functional interface
public
public interface
interface Function
Function < T , R > {
R apply ( T t );
}
a. Can you draw this functional interface diagrammatically?
b. What kind of lambda expressions might you use this functional interface for if
you were writing a software calculator?
c. Which of these lambda expressions are valid Function<Long,Long> imple-
mentations?
x -> x + 1 ;
( x , y ) -> x + 1 ;
x -> x == 1 ;
2. ThreadLocal lambda expressions . Java has a class called ThreadLocal that acts as a
container for a value that's local to your current thread. In Java 8 there is a new fact-
Search WWH ::




Custom Search