Translate

Friday, October 14, 2016

Java 8 lambda expression made easy

Getting Started with Lambda expressions introduced in Java 8:

Lambda expression facilitates functional programming in Java. Lambda expression is a function which can be created without belonging to any class. Lambda expression can be passed around as if it was an object and executed on demand as method reference.

Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Following sample program demonstrates serious ways of using lamda expression.

Sample program:

class LambdaExample {
    private static int i;
    public static void commonMethod() {
        System.out.println("This method is already defined." + ++i);//can i use static method as lambda method reference?
    }
   
    public LambdaExample() {
        System.out.println("LambdaExample constructor called");
    }

    public static void main(String[] args) {

        // Anonymous class
        FuncIface demoOne = new FuncIface() {
            @Override
            public void referenceDemo() {
                LambdaExample.commonMethod();
                System.out.println("LambdaExample -> " + this);//What will this print?
            }
        };
        demoOne.referenceDemo();

        // Lambda implementation
        FuncIface demo = () -> LambdaExample.commonMethod();
        demo.referenceDemo();

        // Static Method reference
        FuncIface demoTwo = LambdaExample::commonMethod;
        demoTwo.referenceDemo();
       
        // Instance method reference
        LambdaExample o = new LambdaExample();
        FuncIface demoThree = o::m1;
        demoThree.referenceDemo();
       
        // Method reference.
        FuncIface demoFour = () -> {
            LambdaExample.commonMethod();
            System.out.println(++i);
        };
        demoFour.referenceDemo();
               
        // ::new example
        FuncIface demo4 = LambdaExample::new;
        FuncIface demo5 = String::new;
       
    }
   
    public void m1() {
        System.out.println("m1() called");
    }

}


Output:

This method is already defined.1
LambdaExample -> LambdaExample$1@15db9742
This method is already defined.2
This method is already defined.3
LambdaExample constructor called
m1() called
This method is already defined.4
5


References:

Java 8 lambda expression made easy

Getting Started with Lambda expressions introduced in Java 8:

Lambda expression facilitates functional programming in Java. Lambda expression is a function which can be created without belonging to any class. Lambda expression can be passed around as if it was an object and executed on demand as method reference.

Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. Following demonstrates serious ways of using lamda expression.

Sample program:

class LambdaExample {
    private static int i;
    public static void commonMethod() {
        System.out.println("This method is already defined." + ++i);//can i use static method as lambda method reference?
    }
   
    public LambdaExample() {
        System.out.println("LambdaExample constructor called");
    }

    public static void main(String[] args) {

        // Anonymous class
        FuncIface demoOne = new FuncIface() {
            @Override
            public void referenceDemo() {
                LambdaExample.commonMethod();
                System.out.println("LambdaExample -> " + this);//What will this print?
            }
        };
        demoOne.referenceDemo();

        // Lambda implementation
        FuncIface demo = () -> LambdaExample.commonMethod();
        demo.referenceDemo();

        // Static Method reference
        FuncIface demoTwo = LambdaExample::commonMethod;
        demoTwo.referenceDemo();
       
        // Instance method reference
        LambdaExample o = new LambdaExample();
        FuncIface demoThree = o::m1;
        demoThree.referenceDemo();
       
        // Method reference.
        FuncIface demoFour = () -> {
            LambdaExample.commonMethod();
            System.out.println(++i);
        };
        demoFour.referenceDemo();
               
        // ::new example
        FuncIface demo4 = LambdaExample::new;
        FuncIface demo5 = String::new;
       
    }
   
    public void m1() {
        System.out.println("m1() called");
    }

}


Output:

This method is already defined.1
LambdaExample -> LambdaExample$1@15db9742
This method is already defined.2
This method is already defined.3
LambdaExample constructor called
m1() called
This method is already defined.4
5


References: