Lambda Expression
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression
• It is an anonymous (that is, unnamed) method
• A lambda expression results in a form of anonymous class.
• This method is not executed on its own. Instead, it is used to implement a
method defined by a functional interface.
• Lambda expressions are also commonly referred to as closures
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression Fundamentals
• The lambda expression introduces a new syntax element and operator into the
Java language
• lambda operator or the arrow operator
• Lambda operator divides the lambda expression into two parts.
• The left side specifies any parameters required by the lambda expression.
• On the right side is the lambda body, which specifies the actions of the lambda
expression
• Syntax:
(param1,param2) -> body
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression Fundamentals
• lambda expression consist of three components:
▪ Argument-list: It can be empty or non-empty as well.
▪ Arrow-token: It is used to link arguments-list and body of expression.
▪ Body: It contains expressions and statements for lambda expression.
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression
• No Parameter Syntax
() -> {
//Body of no parameter lambda
}
• One Parameter Syntax
(param1) -> {
//Body of no parameter lambda
}
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression
Multiple parameter Syntax
(param1, param2) -> {
//Body of no parameter lambda
}
Dr.M.S.Bhuvaneswari, SCORE,VIT
Functional Interface
• A functional interface is an interface that contains one and only one abstract method.
Example: Runnable is a functional interface because it defines only one method
run( )
• It typically represents a single action.
• A functional interface defines the target type of a lambda expression.
• When a lambda expression occurs in a target type context, an instance of a class is
automatically created that implements the functional interface, with the lambda
expression defining the behavior of the abstract method declared by the functional
interface
• A functional interface is sometimes referred to as a SAM type, where SAM stands for
Single Abstract Method.
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression Fundamentals
• Java defines two types of lambda bodies.
▪ single expression
▪ block of code
Dr.M.S.Bhuvaneswari, SCORE,VIT
Normal Method
=============
int findSum()
{
return 20;
}
Lambda Expression
================
() -> 20
Lambda Expression
• In order for a lambda expression to be used in a target type context, the type of
the abstract method and the type of the lambda expression must be compatible
• Rules
▪ The type and number of the lambda expression’s parameters must be
compatible with the method’s parameters;
▪ The return types must be compatible; and
▪ Any exceptions thrown by the lambda expression must be acceptable to the
method
Dr.M.S.Bhuvaneswari, SCORE,VIT
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf {
public int getValue();
}
class ClsIntf implements FuncIntf
{
public int getValue()
{
return 20;
}
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = new ClsInft();
System.out.println("Value is:"+intfRef.getValue());
}
}
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf {
int getValue();
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = new ClsInft();
System.out.println("Value is:"+intfRef.getValue());
}
}
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf {
int getValue();
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = ;
System.out.println("Value is:"+intfRef.getValue());
}
}
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf {
int getValue();
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = () -> 20;
System.out.println("Value is:"+intfRef.getValue());
}
}
Lambda Expression () -> 20
================
• Implements the method getValue() in FuncIntf
• Returns an instance of the class
Lambda Expression with parameters - Example2
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf
{
int findSum(int a,int b);
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = (x,y)-> {int c=x+y;return c;};
System.out.println("Value is:"+intfRef.findSum(6,8));
}
}
Lambda Expression with parameters type- Example3
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf
{
int findSum(int a,int b);
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = (int x, int y)-> {int c=x+y;return c;};
System.out.println("Value is:"+intfRef.findSum(6,8));
}
}
Lambda Expression with parameters type- Example4
Dr.M.S.Bhuvaneswari, SCORE,VIT
interface FuncIntf
{
int findSum(int a,int b);
}
class MyClass
{
public static void main(String[] args)
{
FuncIntf intfRef = (int x, int y)-> {int c=x+y;return c;}; //with return(with multiple statements)
System.out.println("Value is:"+intfRef.findSum(6,8));
FuncIntf intfRef1 = (int x,int y)-> (x+y); //without return(with only one statement)
System.out.println("Value is:"+intfRef1.findSum(6,8));
}
}
Thread Implementation without Lambda
class MyThread implements Runnable{
public void run()
{
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
}
Dr.M.S.Bhuvaneswari, SCORE,VIT
Thread Implementation without Lambda
Runnable rintf=new MyThread()
Thread th1=new Thread(rintf)
th1.start()
Dr.M.S.Bhuvaneswari, SCORE,VIT
Thread Implementation using Lambda
Thread th1=new Thread(() -> {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
}
catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
});
th1.start();
Dr.M.S.Bhuvaneswari, SCORE,VIT
References
• https://www.javatpoint.com/java-lambda-expressions

Lambda Expression For anyone that needs Java Lambda notes

  • 1.
  • 2.
    Lambda Expression • Itis an anonymous (that is, unnamed) method • A lambda expression results in a form of anonymous class. • This method is not executed on its own. Instead, it is used to implement a method defined by a functional interface. • Lambda expressions are also commonly referred to as closures Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 3.
    Lambda Expression Fundamentals •The lambda expression introduces a new syntax element and operator into the Java language • lambda operator or the arrow operator • Lambda operator divides the lambda expression into two parts. • The left side specifies any parameters required by the lambda expression. • On the right side is the lambda body, which specifies the actions of the lambda expression • Syntax: (param1,param2) -> body Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 4.
    Lambda Expression Fundamentals •lambda expression consist of three components: ▪ Argument-list: It can be empty or non-empty as well. ▪ Arrow-token: It is used to link arguments-list and body of expression. ▪ Body: It contains expressions and statements for lambda expression. Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 5.
    Lambda Expression • NoParameter Syntax () -> { //Body of no parameter lambda } • One Parameter Syntax (param1) -> { //Body of no parameter lambda } Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 6.
    Lambda Expression Multiple parameterSyntax (param1, param2) -> { //Body of no parameter lambda } Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 7.
    Functional Interface • Afunctional interface is an interface that contains one and only one abstract method. Example: Runnable is a functional interface because it defines only one method run( ) • It typically represents a single action. • A functional interface defines the target type of a lambda expression. • When a lambda expression occurs in a target type context, an instance of a class is automatically created that implements the functional interface, with the lambda expression defining the behavior of the abstract method declared by the functional interface • A functional interface is sometimes referred to as a SAM type, where SAM stands for Single Abstract Method. Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 8.
    Lambda Expression Fundamentals •Java defines two types of lambda bodies. ▪ single expression ▪ block of code Dr.M.S.Bhuvaneswari, SCORE,VIT Normal Method ============= int findSum() { return 20; } Lambda Expression ================ () -> 20
  • 9.
    Lambda Expression • Inorder for a lambda expression to be used in a target type context, the type of the abstract method and the type of the lambda expression must be compatible • Rules ▪ The type and number of the lambda expression’s parameters must be compatible with the method’s parameters; ▪ The return types must be compatible; and ▪ Any exceptions thrown by the lambda expression must be acceptable to the method Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 10.
    Lambda Expression Example1 Dr.M.S.Bhuvaneswari,SCORE,VIT interface FuncIntf { public int getValue(); } class ClsIntf implements FuncIntf { public int getValue() { return 20; } } class MyClass { public static void main(String[] args) { FuncIntf intfRef = new ClsInft(); System.out.println("Value is:"+intfRef.getValue()); } }
  • 11.
    Lambda Expression Example1 Dr.M.S.Bhuvaneswari,SCORE,VIT interface FuncIntf { int getValue(); } class MyClass { public static void main(String[] args) { FuncIntf intfRef = new ClsInft(); System.out.println("Value is:"+intfRef.getValue()); } }
  • 12.
    Lambda Expression Example1 Dr.M.S.Bhuvaneswari,SCORE,VIT interface FuncIntf { int getValue(); } class MyClass { public static void main(String[] args) { FuncIntf intfRef = ; System.out.println("Value is:"+intfRef.getValue()); } }
  • 13.
    Lambda Expression Example1 Dr.M.S.Bhuvaneswari,SCORE,VIT interface FuncIntf { int getValue(); } class MyClass { public static void main(String[] args) { FuncIntf intfRef = () -> 20; System.out.println("Value is:"+intfRef.getValue()); } } Lambda Expression () -> 20 ================ • Implements the method getValue() in FuncIntf • Returns an instance of the class
  • 14.
    Lambda Expression withparameters - Example2 Dr.M.S.Bhuvaneswari, SCORE,VIT interface FuncIntf { int findSum(int a,int b); } class MyClass { public static void main(String[] args) { FuncIntf intfRef = (x,y)-> {int c=x+y;return c;}; System.out.println("Value is:"+intfRef.findSum(6,8)); } }
  • 15.
    Lambda Expression withparameters type- Example3 Dr.M.S.Bhuvaneswari, SCORE,VIT interface FuncIntf { int findSum(int a,int b); } class MyClass { public static void main(String[] args) { FuncIntf intfRef = (int x, int y)-> {int c=x+y;return c;}; System.out.println("Value is:"+intfRef.findSum(6,8)); } }
  • 16.
    Lambda Expression withparameters type- Example4 Dr.M.S.Bhuvaneswari, SCORE,VIT interface FuncIntf { int findSum(int a,int b); } class MyClass { public static void main(String[] args) { FuncIntf intfRef = (int x, int y)-> {int c=x+y;return c;}; //with return(with multiple statements) System.out.println("Value is:"+intfRef.findSum(6,8)); FuncIntf intfRef1 = (int x,int y)-> (x+y); //without return(with only one statement) System.out.println("Value is:"+intfRef1.findSum(6,8)); } }
  • 17.
    Thread Implementation withoutLambda class MyThread implements Runnable{ public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 18.
    Thread Implementation withoutLambda Runnable rintf=new MyThread() Thread th1=new Thread(rintf) th1.start() Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 19.
    Thread Implementation usingLambda Thread th1=new Thread(() -> { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); }); th1.start(); Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 20.