Lambda expressions in java are used to instantiate a class with single abstract method in more compact way. We can write the less code using anonymous class concept rather than defining a new class or by using an inner class. Suppose the thread programming in java using Runnable interface can be implemented in many ways. We can define a new class that implements the Runnable interface and then override the run method or by using Inner class concept or by using anonymous class concept like.

java8 lambda expression


  Thread thread = new Thread(new Runnable() {

public void run() {
System.out.println("running");
}
});
        thread.start();

writing the code using anonymous class is also somewhat excessive and cumbersome. we can even write the code less than this by using lambda expressions like

        Runnable runnable = () -> System.out.println("running");
        Thread thread = new Thread(runnable);
        thread.start();

As Lambda expressions are only applied for Single abstract method interfaces the method will be known automatically at run time. The Single abstract Method interfaces are now called as Functional Interfaces in java 8, prior to this they were called as “Single Abstract Method” types. An interface is a functional interface if it has a single abstract method. Lambda expressions are applied to the functional interface type classes only.

Syntax: 
() Method signature.

-> Lambda operator

{} Method Body

Runnable runnable = () -> System.out.println("running");

Here I am creating a Runnable functional interface object. The method signature specifies the run method in the Runnable interface. We no need to declare any function name and also we are not passing any arguments here because the run method doesn’t expect any arguments. If a method expects some arguments then it is enough to give the names no need to declare their types because lambda expression help us to avoid unnecessary syntax. The Lambda operator seperates the method signature from the method body. If method body has only a single statement then can omit the curly braces otherwise need to use them 

Runnable runnable = () ->  {
        System.out.println("running");
        System.out.println("Thread");
        }

Example:
I am defining a functional interface with the name “FunctionalInterfaceDemo”
package com.lambda.exps;

@FunctionalInterface
public interface FunctionalInterfaceDemo {

public void display();
}

We create the functional interface by annotating the interface with “@FunctionalInterface” annotation and by declaring a single abstract method as shown above. Remember that there should be only one abstract method because the Lambda expression are only applied to Single abstract method interfaces.

package com.lambda.exps;
public class LambdaExpsDemo {

/**
* @param args
*/
public static void main(String[] args) {
FunctionalInterfaceDemo demo = () -> System.out.println("java 8 feature lambda                                 expressions demo1"); 
demo.display();
               }
       }
  
Here I am creating FunctionalinterfaceDemo object by using lambda expression and I have ignored the curly braces as I implemented the abstract method with a single print statement. 
package com.lambda.exps;

@FunctionalInterface
public interface FunctionalInterfaceDemo2 {

public void display(String name);
}

Now I am creating another FunctionalInterface but this time the abstract method is declaring with one String argument. Let us see how can we implement this.

package com.lambda.exps;
public class LambdaExpsDemo2 {

/**
* @param args
*/
public static void main(String[] args) {
FunctionalInterfaceDemo2 demoInterface = (name) ->         
   System.out.print(name);
demoInterface.display("java8 ");

demoInterface = name -> System.out.print(name);
demoInterface.display("Feature ");
demoInterface = (name) -> {
name += " expressions demo2";
System.out.print(name);
};
demoInterface.display("Lambda");
   }
   }

As we discussed early we no need to declare the argument types in the method signature. We can also avoid the functional brackets if we have only a single argument in method signature. At first I am creating FunctionalInterfaceDemo2 object by implementing it’s only abstract method. While following the lambda expression I am just giving the argument name and using that argument in the method body

0 comments:

Post a Comment

 
Top