Lamda Expressions (λEs), also known as closures, provides 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 which provide 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 7 parts; Functional Interfaces, Lambda Expressions, Method & Contstructor References, Poly Expressions, Typing & Evaluation, Type Interence andd 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 may have 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.
@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
}
The general usage of Lambda Expressions include a parameter list, a return type and a body.
(
parameter
list
)
->
{
statements
;
}
Examples of λEs include:
()
->
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 once 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"
);
// Anonymous inner class usage
b
.
setOnAction
(
new
EventHandler
<
ActionEvent
>()
{
@Override
public
void
handle
(
ActionEvent
event
)
{
stage
.
setTitle
(
"Title"
);
}
});
StackPane
root
=
new
StackPane
();
root
.
getChildren
().
add
(
b
);
Scene
scene
=
new
Scene
(
root
,
200
,
50
);
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!"
);
});
Reference Comparator Functional Interface to see 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 also include class constructor reference and array constructor reference.
"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 Table 19-1 have been estasblished for specific purposes relative to the packages/APIs in which they are in. Not all functional interfaces in the Java SE API are currently annotated.
API | Class | Method |
AWT |
|
|
AWT |
|
|
IO |
|
|
IO |
|
|
LANG |
|
|
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 Table 19-2 lists them all as there may be times where a developer may need to use them.
Consumer | accept (T t) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
This section provides helful links to tutorials and community resources about λEs.