Lambda Expression in Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Agenda
• Lambda Expression in Java
• Use of Lambda Expression in Java
• How to write Lambda Expression in Java
• Rules for Lambda Expression in Java
• Functional Interface
• Functional Interface implementation
• Functional interface with Lambda expression
• Lambda Expression program in Java
• Lambda Vs Interface
• lambda value Capture
• Create Thread using lambda expression
• Method reference in lambda expression
• Constructor reference in lambda expression
• Exception handling in lambda expression
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Expressions in Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Expressions in Java
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• Lambda expressions are block of code without a name
• Lambda expression Implementation code can be provided when it is called
• It is a representation of anonymous class
• It can be referred as method without a name
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Use of Lambda Expressions in
Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Use of Lambda Expressions in Java
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• Advantage to use it is less coding
• The code to be used exactly once can be wrapped as lambda expression
• Encourages functional Programming
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
How to write Lambda
Expressions in Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
How to write Lambda Expressions in Java
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Syntax:-
1. ( one_parameter ) -> { lambda expression };
2. ( first_parameter , second_parameter ) -> { lambda expression };
3. ( ) -> {lambda expression };
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functional Interface
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functional Interface
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• The interface having single abstract method are called functional interface
• These methods can have n number of static default methods
• Example:- Runnable, Predicate etc.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functional Interface
implementation
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functional Interface implementation
Predicate Interface
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
public interface Predicate<T>
{
boolean test(T t);
}
public class Example implements Predicate
{
@Override
public boolean test(Object o)
{
return o == null;
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functional interface with Lambda
Expressions
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Functional interface with Lambda Expressions
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• Lambda expression can be used for method implementation of one abstract method defined in
Functional Interface.
• Return type of lambda function should be same as interface type
public class Example {
public static void main(String a1[])
{
Predicate<String> p1 = x -> {
if(x == "Hello")
return true;
else
return false;
};
System.out.println(p1.test("Hello"));
}
} Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Rules for Lambda Expressions in
Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Rules for Lambda Expressions in Java
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• The variable type should be of Interface type, where Interface has one method
• The lambda expression should have same number of parameters and same return type as that of
the method in that Interface
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Expressions Program in
Java
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Vs Interface
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Vs Interface
• Lambda is used for implementing the methods of those interfaces who has only one abstract method.
• Lambda cannot be used for interface with more than one abstract methods
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Value Capture
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Lambda Value Capture
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• The returned value from lambda expression can be stored in a variable of type “Interface” i.e.
object of type interface
• The variables declared in the scope of lambda expression can be accessed in lambda expression
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Create Thread using Lambda
Expressions
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Method reference in Lambda
Expression
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Method reference in Lambda Expression
Static method reference
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Method reference in Lambda Expression
Instance method reference
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Constructor reference in Lambda
Expression
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Constructor reference in Lambda Expression
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
Interface StringMessage{
getString(String st);
}
Class StringExample{
StringExample(String s1){
System.out.println(“ Great “ +s1)}
}
public class Example{
public static void main(String args[]){
StringMessage m1 = StringExample :: new;
m1.getString(“Learning platform”);
}
}
Constructor reference
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Exception handling in Lambda
Expression
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Exception handling in Lambda Expression
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
• Exception handling using try-catch for Lambda expression losses the authenticity of lambda
expressions
• A wrapper method can be created that can be used for handling the exception and then pass the
lambda expression as a parameter to this wrapper method
• This wrapper method can also be made generic
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Exception handling in Lambda Expression
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
public class Example {
public static void main(String[] args)
{
Predicate<Integer> p1 = x -> {
try{ if(x%2==0){return true;}
else { return false;}
}catch(Exception e){
System.out.println(e);}
};
System.out.println(p1.test(4)));
}
}
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Exception handling in Lambda Expression
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
public class Example {
static Predicate<Integer> WrapperP(Predicate<Integer> p1)
{
return x -> {
try{ if(x%2==0){return true;}
else { return false;}
};
public static void main(String[] args){
WrapperP((x) -> System.out.println(p1.test(4)));
}
} Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
DO NOT WRITE ANYTHING
HERE. LEAVE THIS SPACE FOR
WEBCAM
We discussed
• Lambda Expression in Java
• It’s Use and How to write Lambda Expression in Java
• Functional Interface and its implementation with Lambda expression
• Lambda Expression program in Java
• Difference between Lambda and Interface
• lambda value Capture
• Creating a Thread using lambda expression
• Method and Constructor reference in lambda expression
• Exception handling in lambda expression
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Thank You
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

LambdaExpressionInjavaforlearning fucntion

  • 1.
    Lambda Expression inJava Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 2.
    DO NOT WRITEANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Agenda • Lambda Expression in Java • Use of Lambda Expression in Java • How to write Lambda Expression in Java • Rules for Lambda Expression in Java • Functional Interface • Functional Interface implementation • Functional interface with Lambda expression • Lambda Expression program in Java • Lambda Vs Interface • lambda value Capture • Create Thread using lambda expression • Method reference in lambda expression • Constructor reference in lambda expression • Exception handling in lambda expression Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 3.
    Lambda Expressions inJava Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 4.
    Lambda Expressions inJava DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • Lambda expressions are block of code without a name • Lambda expression Implementation code can be provided when it is called • It is a representation of anonymous class • It can be referred as method without a name Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 5.
    Use of LambdaExpressions in Java Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 6.
    Use of LambdaExpressions in Java DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • Advantage to use it is less coding • The code to be used exactly once can be wrapped as lambda expression • Encourages functional Programming Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 7.
    How to writeLambda Expressions in Java Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 8.
    How to writeLambda Expressions in Java DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Syntax:- 1. ( one_parameter ) -> { lambda expression }; 2. ( first_parameter , second_parameter ) -> { lambda expression }; 3. ( ) -> {lambda expression }; Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 9.
    Functional Interface Proprietary content.©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 10.
    Functional Interface DO NOTWRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • The interface having single abstract method are called functional interface • These methods can have n number of static default methods • Example:- Runnable, Predicate etc. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 11.
    Functional Interface implementation Proprietary content.©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 12.
    Functional Interface implementation PredicateInterface DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM public interface Predicate<T> { boolean test(T t); } public class Example implements Predicate { @Override public boolean test(Object o) { return o == null; } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 13.
    Functional interface withLambda Expressions Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 14.
    Functional interface withLambda Expressions DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • Lambda expression can be used for method implementation of one abstract method defined in Functional Interface. • Return type of lambda function should be same as interface type public class Example { public static void main(String a1[]) { Predicate<String> p1 = x -> { if(x == "Hello") return true; else return false; }; System.out.println(p1.test("Hello")); } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 15.
    Rules for LambdaExpressions in Java Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 16.
    Rules for LambdaExpressions in Java DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • The variable type should be of Interface type, where Interface has one method • The lambda expression should have same number of parameters and same return type as that of the method in that Interface Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 17.
    Lambda Expressions Programin Java Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 18.
    Lambda Vs Interface Proprietarycontent. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 19.
    Lambda Vs Interface •Lambda is used for implementing the methods of those interfaces who has only one abstract method. • Lambda cannot be used for interface with more than one abstract methods DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 20.
    Lambda Value Capture Proprietarycontent. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 21.
    Lambda Value Capture DONOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • The returned value from lambda expression can be stored in a variable of type “Interface” i.e. object of type interface • The variables declared in the scope of lambda expression can be accessed in lambda expression Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 22.
    Create Thread usingLambda Expressions Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 23.
    Method reference inLambda Expression Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 24.
    Method reference inLambda Expression Static method reference DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 25.
    Method reference inLambda Expression Instance method reference DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 26.
    Constructor reference inLambda Expression Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 27.
    Constructor reference inLambda Expression DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM Interface StringMessage{ getString(String st); } Class StringExample{ StringExample(String s1){ System.out.println(“ Great “ +s1)} } public class Example{ public static void main(String args[]){ StringMessage m1 = StringExample :: new; m1.getString(“Learning platform”); } } Constructor reference Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 28.
    Exception handling inLambda Expression Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 29.
    Exception handling inLambda Expression DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM • Exception handling using try-catch for Lambda expression losses the authenticity of lambda expressions • A wrapper method can be created that can be used for handling the exception and then pass the lambda expression as a parameter to this wrapper method • This wrapper method can also be made generic Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 30.
    Exception handling inLambda Expression DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM public class Example { public static void main(String[] args) { Predicate<Integer> p1 = x -> { try{ if(x%2==0){return true;} else { return false;} }catch(Exception e){ System.out.println(e);} }; System.out.println(p1.test(4))); } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 31.
    Exception handling inLambda Expression DO NOT WRITE ANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM public class Example { static Predicate<Integer> WrapperP(Predicate<Integer> p1) { return x -> { try{ if(x%2==0){return true;} else { return false;} }; public static void main(String[] args){ WrapperP((x) -> System.out.println(p1.test(4))); } } Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 32.
    Summary DO NOT WRITEANYTHING HERE. LEAVE THIS SPACE FOR WEBCAM We discussed • Lambda Expression in Java • It’s Use and How to write Lambda Expression in Java • Functional Interface and its implementation with Lambda expression • Lambda Expression program in Java • Difference between Lambda and Interface • lambda value Capture • Creating a Thread using lambda expression • Method and Constructor reference in lambda expression • Exception handling in lambda expression Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 33.
    Thank You Proprietary content.©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited