LAMBDA - Java

The Lambda expression is used to provide the implementation of an interface which has functional interface. It saves a lot of code. In case of lambda expression, we don't need to define the method again for providing the implementation.

Functional Interface

An interface which has only one abstract method is called functional interface

Implementation of functional Interface
Lambda Provides Less Coding
Syntax:
(argument-list) -> {body}  

Argument-list: It can be empty or non-empty as well.
Arrow: It is used to link arguments-list and body of expression.
Body: It contains expressions and statements for lambda expression.

No Parameter Syntax
( ) -> {  
//Body of no parameter lambda  
}  
One Parameter Syntax
(p1) -> {  
//Body of single parameter lambda  
}  
Two Parameter Syntax
(p1,p2) -> {  
//Body of multiple parameter lambda  
}  

Program on interface with anonymous inner class(not using lambda)
interface abc{ 
public void put(); 

public class sample { 
public static void main(String[] args) { 
int a=10; 

//without lambda, abc implementation using anonymous class 
abc x=new abc(){ 
public void put(){System.out.println("abc put"+a);} 
}; 
x.put(); 

Program with LAMBDA
interface abc{ 
public void put(); 

public class sample { 
public static void main(String[] args) { 
int a=10; 

abc x=()->{ 
System.out.println("abc put"+a);
}; 
x.put(); 


LAMBDA With return
interface abc{ 
public int put(); 


public class sample { 
public static void main(String[] args) { 
int a=10; 

abc x=()->{ 
return a;
}; 
int k=x.put(); 
System.out.println("valueof k"+k);


Program on Parameterized LAMBDA
interface abc{ 
public int put(int x,int y); 

public class sample { 
public static void main(String[] args) { 
int a=10; 
int b=20;
abc x=(m,n)->{ 
return m+n;
};
abc y=(m,n)->(m+n);// or abc y=(int m,int n)->(m+n);
int s=y.put(5,6);
System.out.println("Sum "+s);
int k=x.put(a,b); 
System.out.println("sum "+k);

Functional Interface using LAMBDA to find all simple mathematical calculations.
import java.util.*;
interface abc{ 
public int put(int x,int y); 


class xyz
{
public int result(int x,int y,abc z)
{
return(z.put(x,y));
}
}
public class sample { 
public static void main(String[] args) { 
int a=10; 
int b=20;

abc sum=(m,n)->(m+n);
abc sub=(m,n)->(m-n);
abc mul=(m,n)->(m*n);
abc div=(m,n)->(m/n);
xyz z=new xyz();
System.out.println("Sum of2nos"+z.result(a,b,sum));
System.out.println("suractuib btof2nos"+z.result(a,b,sub));
System.out.println("Multiplication of2nos"+z.result(a,b,mul));
System.out.println("Division of2nos"+z.result(a,b,div));

LAMBDA With forEach loop.
import java.util.*; 
public class sample{ 
public static void main(String[] args) { 

ArrayList<String> lst=new ArrayList<String>(); 

lst.add("prasad"); 
lst.add("vision"); 
lst.add("mounika"); 
lst.add("vamsi"); 

lst.forEach( 
(n)->System.out.println(n) 
);
ArrayList <Integer>lst1=new ArrayList<Integer>();
lst1.add(10);
lst1.add(11);
lst1.add(12);
lst1.add(13);
lst1.add(14);
lst1.forEach(n->{
if(n%2==0) System.out.println(n);
}
);
}

Threading Using LAMBDA
public class sample{ 
public static void main(String[] args) { 

//Thread Example without lambda 
Runnable r1=new Runnable(){ 
public void run(){ 
System.out.println("Vision Thread is running..."); 

}; 
Thread t1=new Thread(r1); 
t1.start(); 
//Thread Example with lambda 
Runnable r2=()->{ 
System.out.println("prasad Thread is running..."); 
}; 
Thread t2=new Thread(r2); 
t2.start(); 

 

Advertisements

For
Online Classes

Contact Us: 9885348743