SlideShare a Scribd company logo
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

More Related Content

PPTX
Java 8 features
PDF
Unit-3.pptx.pdf java api knowledge apiii
PPTX
Lambdas, Collections Framework, Stream API
PDF
PDF
Lambdas in Java 8
PPTX
Java Lambda Expressions.pptx
PDF
Java 8 features
PPT
Major Java 8 features
Java 8 features
Unit-3.pptx.pdf java api knowledge apiii
Lambdas, Collections Framework, Stream API
Lambdas in Java 8
Java Lambda Expressions.pptx
Java 8 features
Major Java 8 features

Similar to Lambda Expression For anyone that needs Java Lambda notes (20)

PPTX
PDF
LambdaExpressionInjavaforlearning fucntion
PDF
Java 8 Lambda Expressions & Streams
PPTX
Java gets a closure
PPTX
Java 8 lambda
PDF
Jf12 lambdas injava8-1
PDF
Functional programming in java 8 by harmeet singh
ODP
Java8 Lambda and Streams
PDF
Lambda Expressions in Java
PDF
Programming with Lambda Expressions in Java
PPTX
Project Lambda: Evolution of Java
PPTX
Java 8 Lambda Expressions
PDF
Project Lambda, JSR 335
ODP
Method Handles in Java
PPTX
PPTX
Java 8 Bootcamp
PPTX
Simple Lambdas in java in oca 8.0 on feb
PPTX
Java Advanced Topic - Java Lambda Expressions.pptx
PPTX
Fun with lambda expressions
PPTX
Java 8 new features
LambdaExpressionInjavaforlearning fucntion
Java 8 Lambda Expressions & Streams
Java gets a closure
Java 8 lambda
Jf12 lambdas injava8-1
Functional programming in java 8 by harmeet singh
Java8 Lambda and Streams
Lambda Expressions in Java
Programming with Lambda Expressions in Java
Project Lambda: Evolution of Java
Java 8 Lambda Expressions
Project Lambda, JSR 335
Method Handles in Java
Java 8 Bootcamp
Simple Lambdas in java in oca 8.0 on feb
Java Advanced Topic - Java Lambda Expressions.pptx
Fun with lambda expressions
Java 8 new features
Ad

Recently uploaded (20)

PPTX
A slide for students with the advantagea
PPTX
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
PPTX
_+✅+JANUARY+2025+MONTHLY+CA.pptx current affairs
PDF
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
PPTX
The Stock at arrangement the stock and product.pptx
PPTX
microtomy kkk. presenting to cryst in gl
PPTX
PMP (Project Management Professional) course prepares individuals
PDF
Blue-Modern-Elegant-Presentation (1).pdf
PPT
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
PPTX
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
PDF
Biography of Mohammad Anamul Haque Nayan
PPTX
internship presentation of bsnl in colllege
PDF
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
PPTX
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
PDF
MCQ Practice CBT OL Official Language 1.pptx.pdf
PDF
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
PDF
L-0018048598visual cloud book for PCa-pdf.pdf
DOCX
mcsp232projectguidelinesjan2023 (1).docx
PPTX
Definition and Relation of Food Science( Lecture1).pptx
PDF
Sales and Distribution Managemnjnfijient.pdf
A slide for students with the advantagea
AREAS OF SPECIALIZATION AND CAREER OPPORTUNITIES FOR COMMUNICATORS AND JOURNA...
_+✅+JANUARY+2025+MONTHLY+CA.pptx current affairs
313302 DBMS UNIT 1 PPT for diploma Computer Eng Unit 2
The Stock at arrangement the stock and product.pptx
microtomy kkk. presenting to cryst in gl
PMP (Project Management Professional) course prepares individuals
Blue-Modern-Elegant-Presentation (1).pdf
BCH3201 (Enzymes and biocatalysis)-JEB (1).ppt
FINAL PPT.pptx cfyufuyfuyuy8ioyoiuvy ituyc utdfm v
Biography of Mohammad Anamul Haque Nayan
internship presentation of bsnl in colllege
Why Today’s Brands Need ORM & SEO Specialists More Than Ever.pdf
Autonomic_Nervous_SystemM_Drugs_PPT.pptx
MCQ Practice CBT OL Official Language 1.pptx.pdf
シュアーイノベーション採用ピッチ資料|Company Introduction & Recruiting Deck
L-0018048598visual cloud book for PCa-pdf.pdf
mcsp232projectguidelinesjan2023 (1).docx
Definition and Relation of Food Science( Lecture1).pptx
Sales and Distribution Managemnjnfijient.pdf
Ad

Lambda Expression For anyone that needs Java Lambda notes

  • 2. 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
  • 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 • No Parameter 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 parameter Syntax (param1, param2) -> { //Body of no parameter lambda } Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 7. 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
  • 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 • 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
  • 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 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)); } }
  • 15. 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)); } }
  • 16. 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)); } }
  • 17. 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
  • 18. Thread Implementation without Lambda Runnable rintf=new MyThread() Thread th1=new Thread(rintf) th1.start() Dr.M.S.Bhuvaneswari, SCORE,VIT
  • 19. 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