Lambda Expression

Lambda ExpressionLambda Expression introduces in Java 8 to use Functional Programming with existing Object-Oriented Programing. It provides the inline implementation of a Functional Interface.

Lisp was the first programing language to use Lambda Expression

it's a short block of code that takes input parameters, processes them and returns results accordingly. It's more like an anonymous function but without any name can be implemented within the body of another method which reduces a lot of redundant boilerplate code.

it reduces the length of code and increases readability, maintainability, and concise code. it helps to resolve the complexity of the Anonymous Inner Class to some extent.

Lambda Expression is not a Replacement for Anonymous Inner Class

Syntex of a lambda expression is 

FunctionalInterface instanceVariable = (input parameters) -> {
execution code;
return statement;
}

A functional Interface can be used to write lambda expressions.

package org.wesome.java8;

@FunctionalInterface
interface Apple {
    int operation(int apple1, int apple2);
}

class Fuji {
    public static void main(String args[]) {

        //Lambda Expression with return statement and curly braces
        Apple addition = (int apple1, int apple2) -> {
            return apple1 + apple2;
        };

        //Lambda Expression without return statement and without curly braces
        Apple addition1 = (int apple1, int apple2) -> apple1 + apple2;

        //Lambda Expression without input parameter type declaration
        Apple addition2 = (apple1, apple2) -> apple1 + apple2;

        System.out.println("1 apple + 1 apple = " + addition.operation(1, 1));
        System.out.println("1 apple + 1 apple = " + addition1.operation(1, 1));
        System.out.println("1 apple + 1 apple = " + addition2.operation(1, 1));

    }
}

Lambda Expression allows functions to be assigned or passed as a variable.

package org.wesome.java8;

interface Apple {
    void show();
}

class Fuji {
    public static void main(String[] args) {
        /*  lambda method is assigning  to a variable   */
        Apple apple = () -> System.out.println("i love apple");
        apple.show();

        /*  lambda method is passed as a variable to the constructor   */
        Thread thread = new Thread(() -> System.out.println("i love apple in Lambda Expression Thread"));
        thread.start();
    }
}

 

Benifits of Lambda Expression

Lambda Expression provides inline implementation to the Functional Interfaces.

package org.wesome.java8;

@FunctionalInterface
interface Apple {
    void show();
}

class Fuji {
    public static void main(String[] args) {
        Apple apple = () -> {
            System.out.println("i love Apple");
        };
        /*  since it's a single line method hence input type return and brackets are not required   */
        Apple apple1 = () -> System.out.println("i love Apple");
        apple.show();
        apple1.show();
    }
}

Anonymous class requires a redefinition of method and a lot of boilerplate code, Lambda Expression doesn't require method definition and can be directly implemented.

package org.wesome.java8;

@FunctionalInterface
interface Apple {
    void show();
}

class Fuji {
    public static void main(String[] args) {
        /*-----------------------Anonymous Class-----------------------*/
        Apple apple = new Apple() {
            @Override
            public void show() {
                System.out.println("i love Apple");
            }
        };
        apple.show();

        /*-----------------------Lambda Expression-----------------------*/
        Apple apple1 = () -> System.out.println("i love Apple");
        apple1.show();
    }
}

Lambda Expression provides Stream API, which executes in sequential as well as in parallel order also.

 

Anonymous class generates separate class files whereas Lambda Expression doesn't

Annoymous inner class are almost the same as a normal class but without a name, so the compiler will generate separate class files for them with the parent class name.

package org.wesome.java8;

interface Apple {
    void show();
}

public class Fuji {

    public static void main(String[] args) {
        Apple a = new Apple() {
            @Override
            public void show() {
                System.out.println("i love Apple");
            }
        };
        a.show();
    }
}

javac Fuji.java will generate Apple.class, Fuji$1.class and Fuji.class

The more anonymous class is used in Program, the more class files will be created.

More Class files will create a bigger jar file.

heavy jar fill will take more time to execute.

reference to the Class file will create more garbage.

more garbage would require more garbage collection.

 

Lambda expressions are considered as Anonymous functions or inline functions hence compiler doesn't create separate class files for them.

package org.wesome.java8;

interface Apple {
    void show();
}

public class Fuji {

    public static void main(String[] args) {
        Apple apple = () -> {
            System.out.println("i love Apple");
        };
        apple.show();
    }
}

javac Fuji.java will generate Apple.class and Fuji.class

javap -c Fuji.class will show that all the Lambda Expression call is delegated to invokedynamics. Lambda Expression might become a static method depending on the context, an instance method or just routing to an another existing method in different classes.

follow us on