Java Lambda
Java 8 Features - Functional Interface, Lambda and Method Reference.
Functional Interface
A functional interface is an interface that contains only one abstract method. @FunctionalInterface
annotation is used to ensure that the functional interface can’t have more than one abstract method.
1 | @FunctionalInterface |
General purpose functional interfaces are defined in java.util.function package.
Lambda Expression
Lambda expression is a block of code you can pass around. It can be viewed as instance of functional interface. Lambda can be assigned to functional interface if the parameter types and return types are matched.
Syntax
1 | parameter -> expression body |
Lambda Expression Examples:
1 | Predicate<String> isLowercase = (String s) -> { |
Type Inference
If the parameter types can be inferred, then you don’t need to provide the parameter types.
1 | Predicate<String> isLowercase = s -> { |
Function Body
If the function body consiste of multiple lines, you need to enclose the function body with { }. Otherwise, you can omit the { }
1 | Runnable r = () -> System.out.println("Hello World"); |
If the function body is a return statement only, then return can be omit too.
1 | Predicate<String> isLowercase = s -> StringUtils.isAllUpperCase(s); |
Usage
Usage 1: lambda expression can be used to replace anonymous inner class. so
1 | btn.setOnAction(new EventHandler<ActionEvent>() { |
Can be replaced with
1 | btn.setOnAction( event -> System.out.println("Hello World!")); |
Usage 2: Use together with Stream to process data.
1 | public static void main(String[] args) { |
Variable Capture
Lambda can capture the value of a local variable in the enclosing scope. The captured local variables must be decalred final or effectively final.
However, Lambda can access and modify instance variable of the enclosing class just like anonymous class.
Example:
1 | import javafx.application.Application; |
Here myLocal
must be final or effectively final in order for lambda expression to access it. Instance variable myInstance
does not have the restriction.
Method Reference
Method reference is a subset of lambda expression. A method reference is the shorthand syntax for a lambda expression
that executes just ONE method. So It is possible to use method reference to replace a lambda expression, but not always.
Method Reference systax
1 | ClassName::methodName |
See example
1 | public static void main(String[] args) { |
Here String::toUpperCase
is equivalent to lambda s -> s.toUpperCase()
You can use method reference on constructors
1 | List<String> ids = Arrays.asList("a12", "b34", "c56"); |