Lambda expression in Java. In English

                                       LAMBDA EXPRESSION

Lambda expressions are essentially ways for Java programmers to represent instances of functional interfaces (a functional interface is an interface that has just one abstract method). In Java, lambda expressions and lambda functions are synonymous. A lambda expression is a brief code block that takes in parameters and outputs a value. Java SE 8 has recently incorporated Lambda Expressions. 

Functional interfaces are implemented by Lambda Expressions since they implement the single abstract function. Java 8 introduced lambda expressions, which offer the following features.

Permit the handling of code as data or functionality as a method argument.

A function that is not class-specific and can be built.

Lambda expressions can be executed on demand and passed around just like any other object.

Syntax: -

      lambda operator -> body


Example: - without lambda expression

  1. interface Show{  
  2.     abstract void show();  
  3. }  
  4. class Lambda implements show {  
  5.     public static void main(String[] args) {  
  6.         int value=10;    
  7.        Show s=new Lambda(); 
  8.         void show() {
  9.           System.out.println("Showing: "+value);
  10.         }
  11.         s.show();  
  12.     }  
  13. }  

Output - Showing: 10

             With lambda expression

  1. interface Show{  
  2.     abstract void show();  
  3. }  
  4. class Lambda implements show {  
  5.     public static void main(String[] args) {  
  6.         int value=10;    
  7.        Show s= ( ) -> {
  8.           System.out.println("Showing: "+value);
  9.         };
  10.         s.show();  
  11.     }  
  12. }  

Output - Showing: 10

There are three Lambda Expression Parameters.

  1. Zero Parameter
() -> System.out.println("Zero parameter lambda");

Example: -

  1. interface Say{  
  2.     public String say();  
  3. }  
  4. public class Lambda{  
  5. public static void main(String[] args) {  
  6.     Sayable s=()->{  
  7.         return "I have nothing to say.";  
  8.     };  
  9.     System.out.println(s.say());  
  10. }  
  11. }  

Output - I have nothing to say

  1. Single Parameter
(p) -> System.out.println("One parameter: " + p);

Example: -
  1. interface Say{  
  2.     public String say(String name);  
  3. }  
  4.   
  5. public class Lambda{  
  6.     public static void main(String[] args) {  
  7.         Sayable s1=(name)->{  
  8.             return "Hello "+name;  
  9.         };  
  10.         System.out.println(s1.say("Java"));    
  11.         Sayable s2= name ->{  
  12.             return "Hello, "+name;  
  13.         };  
  14.         System.out.println(s2.say("Java"));  
  15.     }  
  16. }  

Output - Hello Java
             Hello Java

  1. Multiple Parameters
(p1, p2) -> System.out.println("Multiple parameters: "+ p1 + ", " + p2);

Example: -
  1. interface Add{  
  2.     int add(int x,int y);  
  3. }  
  4.   public class Lambda{  
  5.     public static void main(String[] args) {  
  6.         Add ad1=(x,y)->(x+y);  
  7.         System.out.println(ad1.add(5,10));    
  8.         Add ad2=(int x,int y)->(x+y);  
  9.         System.out.println(ad2.add(50,100));  
  10.     }  
  11. }  

Output - 15
            150

Comments

Popular posts from this blog

Loops in Java. In English

Conditions in java. In English