The lambda expression is an anonymous, unnamed method.
It is a virtual functional interface which declares a simple method, usually passed as an argument. That is, lambda expressions are not executed by their own.
It is derived from functional interface (Single Abstract Method or SAM) in Java8.
By using it, the readability of passed methods is increasing while the code amount is greatly decreasing.
It is used to implement a method without an explicitly defined functional interface.
General formula:
(virtual parameter list of the abstract method) -> unique implemented algorithm of the abstract method
or in other way:
(parameter list) -> lambda body
In case the body consists of more statements:
(parameter list) -> {lambda body}
In case the body consists of 1 statement, the body is called expression body.
In case the body consists of more statements, the body is called block body (avoid it by organizing the statements into an external helper method for better code).
In case there is only one parameter, using braces becomes optional (do not put one parameter into braces for better code).
In case of no parameter:
() -> statement
In case of 1 parameter:
parameter -> statement
In case of more parameters:
(parameter1, parameter2, ...) -> statement
EXAMPLE FOR DERIVING 0-STATEMENT SAM INTO LAMBDA:
EXAMPLE FOR DERIVING 1-STATEMENT SAM INTO LAMBDA:
EXAMPLE FOR DERIVING 1-STATEMENT-SAM-WITH-RETURN-VALUE INTO LAMBDA:
EXAMPLE FOR DERIVING MORE-STATEMENT SAM INTO LAMBDA:
EXAMPLE FOR DERIVING MORE-STATEMENT-SAM-WITH-RETURN-VALUE INTO LAMBDA:
In case the expression will be used more than once (not just in-place), must a functional interface be declared which will carry the block of the lambda.
The functional interface then can be assigned to a variable which will represent an anonymous class.
The variable is not an instance since the reference points to an interface is not a class.
That variable can be passed as parameter.
EXAMPLE FOR USING LAMBDA THROUGH FUNCTIONAL INTERFACE IN MORE PLACES:
EXAMPLE FOR LAMBDA USED NOT IN-PLACE, HANDLING STRING WITH RETURN VALUE:
In case the lambda is used only in-place, declaring the interface and using a reference on it is optional, not necessary.
EXAMPLE FOR USING LAMBDA IN-PLACE:
Lambda can be used with generics functional interface.
Declaring the variable pointing to the instance of such interface will define the carried input type.
Explicitly declaring the type when creating instance is optional, without it Java chooses the best fitting type.
EXAMPLE FOR USING LAMBDA WITH GENERICS FUNCTIONAL INTERFACE:
For lambdas which use external methods as statements, method reference can be used instead of the lambda formula.
In these cases, 0 or 1 parameter is accepted.
Formulas of lambda:
() -> [ClassName.]methodName1()
(parameter) -> [ClassName.]methodName2(parameter)
Replacer formulas of method reference:
ClassName::methodName1
ClassName::methodName2
This exchange can increase code readability in case the ClassName is a relatively short one.
In case the ClassName is too long, use the original lambda instead.
EXAMPLE FOR CONVERTING LAMBDA TO METHOD REFERENCE:
Methods can receive lambdas (method references) as parameter.
For this, the type on receiver side should be Runnable (no return value) or Callable (with return value) or an implementation of them.
EXAMPLE FOR PASSING LAMBDA AS ARGUMENT: