SlidePub
Home
Categories
Login
Register
Home
General
Lambda Expression For anyone that needs Java Lambda notes
Lambda Expression For anyone that needs Java Lambda notes
shreyansh15388
20 views
20 slides
Jul 07, 2024
Slide
1
of 20
Previous
Next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
About This Presentation
Java Lambda expression concept
Size:
210.94 KB
Language:
en
Added:
Jul 07, 2024
Slides:
20 pages
Slide Content
Slide 1
Lambda Expression
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 2
Lambda Expression
•Itisananonymous(thatis,unnamed)method
•Alambdaexpressionresultsinaformofanonymousclass.
•Thismethodisnotexecutedonitsown.Instead,itisusedtoimplementa
methoddefinedbyafunctionalinterface.
•Lambdaexpressionsarealsocommonlyreferredtoasclosures
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 3
Lambda Expression Fundamentals
•Thelambdaexpressionintroducesanewsyntaxelementandoperatorintothe
Javalanguage
•lambdaoperatororthearrowoperator
•Lambdaoperatordividesthelambdaexpressionintotwoparts.
•Theleftsidespecifiesanyparametersrequiredbythelambdaexpression.
•Ontherightsideisthelambdabody,whichspecifiestheactionsofthelambda
expression
•Syntax:
(param1,param2)->body
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 4
Lambda Expression Fundamentals
•lambdaexpressionconsistofthreecomponents:
▪Argument-list:Itcanbeemptyornon-emptyaswell.
▪Arrow-token:Itisusedtolinkarguments-listandbodyofexpression.
▪Body:Itcontainsexpressionsandstatementsforlambdaexpression.
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 5
Lambda Expression
•NoParameterSyntax
()->{
//Bodyofnoparameterlambda
}
•OneParameterSyntax
(param1)->{
//Bodyofnoparameterlambda
}
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 6
Lambda Expression
MultipleparameterSyntax
(param1,param2)->{
//Bodyofnoparameterlambda
}
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 7
Functional Interface
•Afunctionalinterfaceisaninterfacethatcontainsoneandonlyoneabstractmethod.
Example:Runnableisafunctionalinterfacebecauseitdefinesonlyonemethod
run()
•Ittypicallyrepresentsasingleaction.
•Afunctionalinterfacedefinesthetargettypeofalambdaexpression.
•Whenalambdaexpressionoccursinatargettypecontext,aninstanceofaclassis
automaticallycreatedthatimplementsthefunctionalinterface,withthelambda
expressiondefiningthebehavioroftheabstractmethoddeclaredbythefunctional
interface
•AfunctionalinterfaceissometimesreferredtoasaSAMtype,whereSAMstandsfor
SingleAbstractMethod.
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 8
Lambda Expression Fundamentals
•Javadefinestwotypesoflambdabodies.
▪singleexpression
▪blockofcode
Dr.M.S.Bhuvaneswari, SCORE,VIT
NormalMethod
=============
intfindSum()
{
return20;
}
LambdaExpression
================
()->20
Slide 9
Lambda Expression
•Inorderforalambdaexpressiontobeusedinatargettypecontext,thetypeof
theabstractmethodandthetypeofthelambdaexpressionmustbecompatible
•Rules
▪Thetypeandnumberofthelambdaexpression’sparametersmustbe
compatiblewiththemethod’sparameters;
▪Thereturntypesmustbecompatible;and
▪Anyexceptionsthrownbythelambdaexpressionmustbeacceptabletothe
method
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 10
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf{
publicintgetValue();
}
classClsIntfimplementsFuncIntf
{
publicintgetValue()
{
return20;
}
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=newClsInft();
System.out.println("Valueis:"+intfRef.getValue());
}
}
Slide 11
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf{
intgetValue();
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=newClsInft();
System.out.println("Valueis:"+intfRef.getValue());
}
}
Slide 12
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf{
intgetValue();
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=;
System.out.println("Valueis:"+intfRef.getValue());
}
}
Slide 13
Lambda Expression Example1
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf{
intgetValue();
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=()->20;
System.out.println("Valueis:"+intfRef.getValue());
}
}
LambdaExpression()->20
================
•ImplementsthemethodgetValue()inFuncIntf
•Returnsaninstanceoftheclass
Slide 14
Lambda Expression with parameters -Example2
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf
{
intfindSum(inta,intb);
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=(x,y)->{intc=x+y;returnc;};
System.out.println("Valueis:"+intfRef.findSum(6,8));
}
}
Slide 15
Lambda Expression with parameters type-Example3
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf
{
intfindSum(inta,intb);
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=(intx,inty)->{intc=x+y;returnc;};
System.out.println("Valueis:"+intfRef.findSum(6,8));
}
}
Slide 16
Lambda Expression with parameters type-Example4
Dr.M.S.Bhuvaneswari, SCORE,VIT
interfaceFuncIntf
{
intfindSum(inta,intb);
}
classMyClass
{
publicstaticvoidmain(String[]args)
{
FuncIntfintfRef=(intx,inty)->{intc=x+y;returnc;}; //withreturn(withmultiplestatements)
System.out.println("Valueis:"+intfRef.findSum(6,8));
FuncIntfintfRef1=(intx,inty)->(x+y); //withoutreturn(withonlyonestatement)
System.out.println("Valueis:"+intfRef1.findSum(6,8));
}
}
Slide 17
Thread Implementation without Lambda
classMyThreadimplementsRunnable{
publicvoidrun()
{
try{
for(inti=5;i>0;i--){
System.out.println("ChildThread:"+i);
Thread.sleep(500);
}
}
catch(InterruptedExceptione){
System.out.println("Childinterrupted.");
}
System.out.println("Exitingchildthread.");
}
}
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 18
Thread Implementation without Lambda
Runnablerintf=newMyThread()
Threadth1=newThread(rintf)
th1.start()
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 19
Thread Implementation using Lambda
Threadth1=newThread(()->{
try{
for(inti=5;i>0;i--){
System.out.println("ChildThread:"+i);
Thread.sleep(500);
}
}
catch(InterruptedExceptione){
System.out.println("Childinterrupted.");
}
System.out.println("Exitingchildthread.");
});
th1.start();
Dr.M.S.Bhuvaneswari, SCORE,VIT
Slide 20
References
•https://www.javatpoint.com/java-lambda-expressions
Tags
Categories
General
Download
Download Slideshow
Get the original presentation file
Quick Actions
Embed
Share
Save
Print
Full
Report
Statistics
Views
20
Slides
20
Age
513 days
Related Slideshows
22
Pray For The Peace Of Jerusalem and You Will Prosper
RodolfoMoralesMarcuc
32 views
26
Don_t_Waste_Your_Life_God.....powerpoint
chalobrido8
33 views
31
VILLASUR_FACTORS_TO_CONSIDER_IN_PLATING_SALAD_10-13.pdf
JaiJai148317
31 views
14
Fertility awareness methods for women in the society
Isaiah47
30 views
35
Chapter 5 Arithmetic Functions Computer Organisation and Architecture
RitikSharma297999
27 views
5
syakira bhasa inggris (1) (1).pptx.......
ourcommunity56
29 views
View More in This Category
Embed Slideshow
Dimensions
Width (px)
Height (px)
Start Page
Which slide to start from (1-20)
Options
Auto-play slides
Show controls
Embed Code
Copy Code
Share Slideshow
Share on Social Media
Share on Facebook
Share on Twitter
Share on LinkedIn
Share via Email
Or copy link
Copy
Report Content
Reason for reporting
*
Select a reason...
Inappropriate content
Copyright violation
Spam or misleading
Offensive or hateful
Privacy violation
Other
Slide number
Leave blank if it applies to the entire slideshow
Additional details
*
Help us understand the problem better