Java 8 - Lambda Expressions


Lambda expressions are introduced in Java 8 and a Lambda expression (or function) is just an anonymous function. i.e., a function with no name and without being bounded to an identifier.

The most important features of Lambda Expressions is that they execute in the context of their appearance. So, a similar lambda expression can be executed differently in some other context (i.e. logic will be same but results will be different based on different parameters passed to function).

Syntax

A lambda expression is characterized by the following syntax.

parameter -> expression body

The other possible syntaxes of a lambda expression are:

either
 
(parameters) -> expression           //1
 
or
 
(parameters) -> { statements; }  //2
 
or
 
() -> expression                     //3

Lambda Expressions Example

Create the following Java program using any editor of your choice in, say, C:\> JAVA.

Java8Tester.java

    
        public class Java8Tester {

    public static void main(String args[]) {
        Java8Tester tester = new Java8Tester();

        //with type declaration
        MathOperation addition = (int x, int y) -> x + y;

        //with out type declaration
        MathOperation subtraction = (x, y) -> x - y;

        //with return statement along with curly braces
        MathOperation multiplication = (int x, int y) -> { return x * y; };

        //without return statement and without curly braces
        MathOperation division = (int x, int y) -> x / y;

        System.out.println("12 + 8 = " + tester.operate(10, 5, addition));
        System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
        System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication));
        System.out.println("10 / 5 = " + tester.operate(10, 5, division));

        //without parenthesis
        GreetingService greetService1 = message ->
        System.out.println("Hello " + message);

        //with parenthesis
        GreetingService greetService2 = (message) ->
        System.out.println("Hello " + message);

        greetService1.sayMessage("SIENNA");
        greetService2.sayMessage("LILY");
    }

    interface MathOperation {
        int operation(int x, int y);
    }

    interface GreetingService {
        void sayMessage(String message);
    }

    private int operate(int x, int y, MathOperation mathOperation) {
        return mathOperation.operation(x, y);
    }
}
    
Run example ยป

Verify the Result

Compile the class using javac compiler as follows โˆ’

C:\JAVA>javac Java8Tester.java

Now run the Java8Tester as follows โˆ’

C:\JAVA>java Java8Tester
It should produce the following output โˆ’
$javac Java8Tester.java
$java -Xmx128M -Xms16M Java8Tester
12 + 8 = 15 10 - 5 = 5 10 x 5 = 50 10 / 5 = 2 Hello SIENNA Hello LILY

Features of Lambda Expressions

  • A lambda expression can have zero, one or more parameters.
  • The type of the parameters can be explicitly declared or it can be inferred from the context.
  • Multiple parameters are enclosed in mandatory parentheses and separated by commas. Empty parentheses are used to represent an empty set of parameters.
  • When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses. e.g. a -> return a*a.





  • javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition