Lambda Expressions
Lambda Expressions
Introduced in JDK 8
Why?
Inc. expressive power of Java
Allowed to add new capabilities into API library
Lambda Expressions : New Capabilities
Ability to take advantage of parallel processing capabilities
For-each style operations
new stream API
Default method
Method reference
Lambda Expressions : Introduction
lambda expression is an anonymous method.
used to impelement a method defined by a functional interface.
also known as closures.
A functional interface is an interface that contains one and only
one abstract method.
for ex. Runnable --------> run()
Lambda Expressions : Introduction
A functional interface defines target type of a lambda expression.
lambda expression can be used only in context where it’s target
type is specified.
A functional interface is also referred as SAM type.
SAM --------> Single Abstract Method
Lambda Expressions : Basics
lambda
operator
0
--->
Parameters required by Lambda
lambda expression. body
verbalized as
“goes to”
w
() ---> 123.45
Lambda Expressions : Basics
() ---> Math.random() * 1000
(n) ---> (n % 2) == 0
n ---> (n % 2) == 0
(int n) ---> (n % 2) == 0
Functional Interface
Prior to JDK 8, all the interface methods were implicitly abstract.
JDK 8, it is possible to specify default behavior for a method
declared in an interface ---- default method
Coz. nondefault methods are implicitly abstract, no need to use
the abstract keyword.
Functional Interface Contd.
lambda expression can be used only in context where it’s target
type is specified.
Contexts:
when a lambda expression is assigned to a functional interface
reference.
variable initialization
retrurn statements
method arguments
more...
Lambda Expression : Internals
// use of lambda in assignment context
when a lambda expression occurs in a target type context:
an instance of a class is automatically created that implements the
functional interface.
coz. lambda expression defines the behavior of the abstract method
declared by functional interface. when the method is called through
the target, the lambda expression is executed.
Lambda Expressions : Type
Single Expression Block Lambda
Expression
() ---> Math.random() * 1000
aka. expression bodies aka.
r block bodies
or or
expression lambdas block lambdas
Lambda Expressions : Block lambdas
Parents
F
lambda
w
body
i
D
win
must have explicit return
statement
Generic Functional Interfaces
Tw
: why?
lambda expression, itself can not specify type parameters.
However, functional interface associated with lambdas can
be generic.
Target type of the lambda expression is determined, in
part, by the type argument(s) specified when the
functional interface reference is declared.
interface GMMyGD
T func Tt
void
interface MyGm2 T R
R func T t
str
HIIIII
instance
Convert to Upperlese
A login to
Passing lambdas as Arguments
w powerfF
Common use of lambdas
It provides a way to pass executable code as an argument
w to a method.
I
a
n
Lambdas and Exceptions
Checked Exception thrown by lambda expression must be
compatible with the exception(s) listed in the throws clause
of functional interface. RuntimeExceptions
Ereepn
checked
gun
Lambdas and Variable Capture
Variables defined by the enclosing scope of a lambda
expression are accessible within the lambda expression.
A lambda can use an instance or static variable defined by
Icf
it’s enclosing class.
Reference
A lambda can also call a method to the
defined by it’s enclosing
current instance of
class. the class
Lambda has also access to this
I d
Lambdas and Variable Capture
when a lambda expression uses a local variable from it’s
enclosing scope, this situation is referred as variable
capture.
students
D
A lambda expression may only use local variable that are
effectively final.
liskshingsE
1M tenewamjli
An effectively final variable is one whoseTT
students.add negetstudate
value doesn’t
stringfane function 1
change after it is first assigned.
studentine
7W
Method References
w
A method reference provides a way to refer to a method
class without executing it.
NumFunc obj new Nump
n
return 4 10 1114151
Method References to static Methods
Toperesolution operator
ClassName::methodName
MyStringOps::strReverse evaluates to an object in which
strReverse provides the implementation of func() in
N
TI
StringFunc.
e
esef inStr
Instead.sn
return reffan instr
Method References to Instancew Methods
objRef::methodName
strOps::strReverse evaluates to an object in which
strReverse provides the implementation of func() in
StringFunc.
w
Constructor References
className::new
It
T
Predefined Functional Interfaces
For most of use-cases, you can you pre-defined functional
interfaces of JDK 8, java.util.function
w T T w
T T T
N void T
C
w R
TW
booke 1