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

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

Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio I
Roan Brasil Monteiro
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
Lambdas
LambdasLambdas
Lambdas
malliksunkara
 
20130329 introduction to linq
20130329 introduction to linq20130329 introduction to linq
20130329 introduction to linq
LearningTech
 
Java gets a closure
Java gets a closureJava gets a closure
Java gets a closure
Tomasz Kowalczewski
 
JDK8 Lambda expressions demo
JDK8 Lambda expressions demoJDK8 Lambda expressions demo
JDK8 Lambda expressions demo
MouryaKumar Reddy Rajala
 
Java 8 Features
Java 8 FeaturesJava 8 Features
Java 8 Features
Leninkumar Koppoju
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
Manav Prasad
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
NexThoughts Technologies
 
Java8 features
Java8 featuresJava8 features
Java8 features
Minal Maniar
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
Sergii Maliarov
 
Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
Syed Awais Mazhar Bukhari
 
Java 8 new features
Java 8 new featuresJava 8 new features
Java 8 new features
Aniket Thakur
 
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Edureka!
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
Bansilal Haudakari
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-Expression
Simplilearn
 
Java moderno java para Jedis episodio I
Java moderno  java para  Jedis  episodio IJava moderno  java para  Jedis  episodio I
Java moderno java para Jedis episodio I
Roan Brasil Monteiro
 
Functional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singhFunctional programming in java 8 by harmeet singh
Functional programming in java 8 by harmeet singh
Harmeet Singh(Taara)
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
Java findamentals1
Java findamentals1Java findamentals1
Java findamentals1
Todor Kolev
 
java150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptxjava150929145120-lva1-app6892 (2).pptx
java150929145120-lva1-app6892 (2).pptx
BruceLee275640
 
20130329 introduction to linq
20130329 introduction to linq20130329 introduction to linq
20130329 introduction to linq
LearningTech
 
Java8: what's new and what's hot
Java8: what's new and what's hotJava8: what's new and what's hot
Java8: what's new and what's hot
Sergii Maliarov
 
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Edureka!
 
Functional Programming In Jdk8
Functional Programming In Jdk8 Functional Programming In Jdk8
Functional Programming In Jdk8
Bansilal Haudakari
 
C#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-ExpressionC#-LINQ-and-Lambda-Expression
C#-LINQ-and-Lambda-Expression
Simplilearn
 

Recently uploaded (20)

最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
Taqyea
 
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn            bvcbvcsuruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn            bvcbvc
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
dineshkumarengg
 
ACEN presentation / huddle from June 2025
ACEN presentation / huddle from June 2025ACEN presentation / huddle from June 2025
ACEN presentation / huddle from June 2025
Mark Rauterkus
 
#5 Selection of the Human resource(1).pptx
#5 Selection of the Human resource(1).pptx#5 Selection of the Human resource(1).pptx
#5 Selection of the Human resource(1).pptx
siddharthchaturvedi0
 
How_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptx
How_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptxHow_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptx
How_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptx
ranjanmuktan
 
Top HR interview question and answer.pdf
Top HR interview question and answer.pdfTop HR interview question and answer.pdf
Top HR interview question and answer.pdf
mbbseo1
 
A Guide for a Winning Interview June 2025
A Guide for a Winning Interview June 2025A Guide for a Winning Interview June 2025
A Guide for a Winning Interview June 2025
Bruce Bennett
 
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
deltaforcedxb7
 
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
Modern trends in Fruit Breedings , A new techniques of fruit BreedingsModern trends in Fruit Breedings , A new techniques of fruit Breedings
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
7300511143
 
B CELL SERIES of B lymphoid lineage.pptx
B CELL SERIES of B lymphoid lineage.pptxB CELL SERIES of B lymphoid lineage.pptx
B CELL SERIES of B lymphoid lineage.pptx
lohanikritika1
 
4099319-Market_Research_on_SKILLCIRCLE.pdf
4099319-Market_Research_on_SKILLCIRCLE.pdf4099319-Market_Research_on_SKILLCIRCLE.pdf
4099319-Market_Research_on_SKILLCIRCLE.pdf
ParimalTripura
 
👉 Top 11 IT Companies in Hinjewadi You Should Know About
👉 Top 11 IT Companies in Hinjewadi You Should Know About👉 Top 11 IT Companies in Hinjewadi You Should Know About
👉 Top 11 IT Companies in Hinjewadi You Should Know About
vaishalitraffictail
 
Academic Preparation for Final Year Externship.pptx
Academic Preparation for Final Year Externship.pptxAcademic Preparation for Final Year Externship.pptx
Academic Preparation for Final Year Externship.pptx
Mark Rauterkus
 
最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制
最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制
最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制
Taqyea
 
Revolutionizing Environmental Compliance with AI.pdf
Revolutionizing Environmental Compliance with AI.pdfRevolutionizing Environmental Compliance with AI.pdf
Revolutionizing Environmental Compliance with AI.pdf
Dorian F Corliss
 
Using Social Media in Job Search June 2025
Using Social Media in Job Search June 2025Using Social Media in Job Search June 2025
Using Social Media in Job Search June 2025
Bruce Bennett
 
最新版英国纽曼大学毕业证(Newman毕业证书)原版定制
最新版英国纽曼大学毕业证(Newman毕业证书)原版定制最新版英国纽曼大学毕业证(Newman毕业证书)原版定制
最新版英国纽曼大学毕业证(Newman毕业证书)原版定制
Taqyea
 
How to Become a CPA USA and Boost Your Career
How to Become a CPA USA and Boost Your CareerHow to Become a CPA USA and Boost Your Career
How to Become a CPA USA and Boost Your Career
ipfcadwords
 
Seniority List of Teachers 2025.pdf......
Seniority List of Teachers 2025.pdf......Seniority List of Teachers 2025.pdf......
Seniority List of Teachers 2025.pdf......
shabrosa35196
 
Solution Elementary Describe hair and eyes
Solution Elementary Describe hair and eyesSolution Elementary Describe hair and eyes
Solution Elementary Describe hair and eyes
NguynNhn615443
 
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
Taqyea
 
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn            bvcbvcsuruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn            bvcbvc
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
dineshkumarengg
 
ACEN presentation / huddle from June 2025
ACEN presentation / huddle from June 2025ACEN presentation / huddle from June 2025
ACEN presentation / huddle from June 2025
Mark Rauterkus
 
#5 Selection of the Human resource(1).pptx
#5 Selection of the Human resource(1).pptx#5 Selection of the Human resource(1).pptx
#5 Selection of the Human resource(1).pptx
siddharthchaturvedi0
 
How_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptx
How_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptxHow_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptx
How_a_hehdhdhdhdhhdhdhdhdndndjdjdjdjdjdjdjdjdjdjdjdjdjdjdjdjd_Works.pptx
ranjanmuktan
 
Top HR interview question and answer.pdf
Top HR interview question and answer.pdfTop HR interview question and answer.pdf
Top HR interview question and answer.pdf
mbbseo1
 
A Guide for a Winning Interview June 2025
A Guide for a Winning Interview June 2025A Guide for a Winning Interview June 2025
A Guide for a Winning Interview June 2025
Bruce Bennett
 
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
deltaforcedxb7
 
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
Modern trends in Fruit Breedings , A new techniques of fruit BreedingsModern trends in Fruit Breedings , A new techniques of fruit Breedings
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
7300511143
 
B CELL SERIES of B lymphoid lineage.pptx
B CELL SERIES of B lymphoid lineage.pptxB CELL SERIES of B lymphoid lineage.pptx
B CELL SERIES of B lymphoid lineage.pptx
lohanikritika1
 
4099319-Market_Research_on_SKILLCIRCLE.pdf
4099319-Market_Research_on_SKILLCIRCLE.pdf4099319-Market_Research_on_SKILLCIRCLE.pdf
4099319-Market_Research_on_SKILLCIRCLE.pdf
ParimalTripura
 
👉 Top 11 IT Companies in Hinjewadi You Should Know About
👉 Top 11 IT Companies in Hinjewadi You Should Know About👉 Top 11 IT Companies in Hinjewadi You Should Know About
👉 Top 11 IT Companies in Hinjewadi You Should Know About
vaishalitraffictail
 
Academic Preparation for Final Year Externship.pptx
Academic Preparation for Final Year Externship.pptxAcademic Preparation for Final Year Externship.pptx
Academic Preparation for Final Year Externship.pptx
Mark Rauterkus
 
最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制
最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制
最新版西班牙巴斯克大学毕业证(UPV毕业证书)原版定制
Taqyea
 
Revolutionizing Environmental Compliance with AI.pdf
Revolutionizing Environmental Compliance with AI.pdfRevolutionizing Environmental Compliance with AI.pdf
Revolutionizing Environmental Compliance with AI.pdf
Dorian F Corliss
 
Using Social Media in Job Search June 2025
Using Social Media in Job Search June 2025Using Social Media in Job Search June 2025
Using Social Media in Job Search June 2025
Bruce Bennett
 
最新版英国纽曼大学毕业证(Newman毕业证书)原版定制
最新版英国纽曼大学毕业证(Newman毕业证书)原版定制最新版英国纽曼大学毕业证(Newman毕业证书)原版定制
最新版英国纽曼大学毕业证(Newman毕业证书)原版定制
Taqyea
 
How to Become a CPA USA and Boost Your Career
How to Become a CPA USA and Boost Your CareerHow to Become a CPA USA and Boost Your Career
How to Become a CPA USA and Boost Your Career
ipfcadwords
 
Seniority List of Teachers 2025.pdf......
Seniority List of Teachers 2025.pdf......Seniority List of Teachers 2025.pdf......
Seniority List of Teachers 2025.pdf......
shabrosa35196
 
Solution Elementary Describe hair and eyes
Solution Elementary Describe hair and eyesSolution Elementary Describe hair and eyes
Solution Elementary Describe hair and eyes
NguynNhn615443
 
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