Lambda expressions (λEs), also known as closures, provide a means to represent anonymous methods. Supported by Project Lambda, λEs allow for the creation and use of single method classes. These methods have a basic syntax that provides for the omission of modifiers, the return type, and optional parameters. The specification for λEs is set out in JSR 335, which is divided into seven parts: functional interfaces, lambda expressions, method and constructor references, poly expressions, typing and evaluation, type inference, and default methods. This chapter focuses on the first two.
λEs must have a functional interface (FI). An FI is an interface that has one abstract method and zero or more default methods. FIs provide target types for lambda expressions and method references, and ideally should be annotated with @FunctionalInterface
to aid the developer and compiler with design intent, as shown in the following code example:
@FunctionalInterface
public
interface
Comparator
<
T
>
{
// Only one abstract method allowed
int
compare
(
T
o1
,
T
o2
);
// Overriding allowed
boolean
equals
(
Object
obj
);
// Optional default methods allowed
}
Lambda expressions typically include a parameter list, a return type, and a body:
(
parameter
list
)
->
{
statements
;
}
Examples of λEs include the following:
()
->
66
(
x
,
y
)
->
x
+
y
(
Integer
x
,
Integer
y
)
->
x
*
y
(
String
s
)
->
{
System
.
out
.
println
(
s
);
}
This simple JavaFX GUI application adds text to the title bar when the button is pressed. The code makes use of the EventHandler
functional interface with the one abstract method, handle()
:
import
javafx.application.Application
;
import
javafx.event.ActionEvent
;
import
javafx.event.EventHandler
;
import
javafx.scene.Scene
;
import
javafx.scene.control.Button
;
import
javafx.scene.layout.StackPane
;
import
javafx.stage.Stage
;
public
class
JavaFxApp
extends
Application
{
@Override
public
void
start
(
Stage
stage
)
{
Button
b
=
new
Button
();
b
.
setText
(
"Press Button to Set Title"
);
// Anonymous inner class usage
b
.
setOnAction
(
new
EventHandler
<
ActionEvent
>()
{
@Override
public
void
handle
(
ActionEvent
event
)
{
stage
.
setTitle
(
"λEs rock!"
);
}
});
StackPane
root
=
new
StackPane
();
root
.
getChildren
().
add
(
b
);
Scene
scene
=
new
Scene
(
root
,
300
,
100
);
stage
.
setScene
(
scene
);
stage
.
show
();
}
public
static
void
main
(
String
[]
args
)
{
launch
();
}
}
To refactor this anonymous inner class into a lambda expression, the parameter type needs to be either (ActionEvent event)
or just (event)
, and the desired functionality needs to be provided as statements in the body:
// Lambda expression usage
b
.
setOnAction
((
ActionEvent
event
)
->
{
stage
.
setTitle
(
"λEs rock!"
);
});
See “Comparator Functional Interface” for another example of lambda expressions with the Comparator
functional interface.
A method reference refers to an existing method without invoking it. Types include static method reference, instance method of particular object, super method of particular object, and instance method of arbitrary object of particular type. Method references are lambda expressions that execute just one method, as demonstrated in the following examples:
"some text"
::
length
// Get length of String
String:
:
length
// Get length of String
CheckAcct:
:
compareByBalance
// Static method ref
myComparator:
:
compareByName
// Inst method part obj
super
::
toString
// Super method part object
String:
:
compareToIgnoreCase
// Inst method arb obj
ArrayList
<
String
>::
new
// New ArrayList constructor
Arrays:
:
sort
// Sort array elements
Annotated FIs listed in Table 19-1 have been established for specific purposes relative to the packages/APIs in which they reside. Not all functional interfaces in the Java SE API are annotated.
API | Class | Method |
---|---|---|
AWT |
|
|
AWT |
|
|
IO |
|
|
IO |
|
|
LANG |
|
|
Nashorn |
|
|
NIO |
|
|
NIO |
|
|
TIME |
|
|
TIME |
|
|
UTIL |
|
|
CONC |
|
|
LOG |
|
|
PREF |
|
|
The java.util.function
package is made up of general-purpose FIs for the primary use of features of the JDK. Table 19-2 lists them all.
Class | Method |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This section provides links to tutorials and community resources about λEs.