Thursday, 7 December 2017

What is lambda Expression in java?

Lambda expressions are the important feature of Java 8
A lambda expression is an anonymous or name less function
The Lambda expression is used to provide the implementation of an interface using an expression.
We don't need to define the method again and again for providing the implementation in lambda expression.
Advantages: less code is required
    Syntax:
  (argument-list) -> {body} 
  Note: it’s a method without a declaration, i.e., access modifier, return value declaration, and name.

Example:
interface Subable{  
  int sub(int x,int y); 
                         }  
  
   public class LambdaDemo{  
  public static void main(String[] args) {  
                  // Multiple parameters in lambda expression  
       Subable sb1=(x,y)->(x-y);  
        System.out.println(sb1.sub(150,20));  
        
       // Multiple parameters with data type in lambda expression  
  Subable sb2=(int x,int y)->(x-y);  
  System.out.println(sb2.sub(100,20));  
    }  
}  


No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...