Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java Programming

Lambda Expressions

Properties of Lambda Expressions :

  • Lambda expressions provides functional programming capability
  • Lambda expressions make the code more compact and readable.
  • If you have a class with a single method, it can be expressed more easily.

Syntax :

  • A Lambda expression contains comma-separated list of formal parameters enclosed in parentheses. Data type of the parameters is optional in a lambda expression. Even you can omit the parentheses in case of one parameter.

    One parameter case :
                    parameter -> System.out.println(parameter)
    Passing multiple parameters with parenthesis :
                    (param1,param2) -> param1+param2

  • Contains arrow token :
                    ->
  • A body, which consists of a single expression or a statement block.

    (param1,param2) -> param1+param2
    Or
    (param1,param2) -> {
    param1+param2;
    }
    Or
    (param1,param2) -> {
    return param1+param2;
    }

  • In case of a single expression, Java runtime evaluates the expression and then returns the value. Alternatively, return statement can be used to return the value. Even if you do not want to return any value that is also acceptable.
    For example operation like printing the value on console.

    displayMessage -> System.out.println(displayMessage)

    Note that a lambda expression looks a lot like a method declaration;
    you can consider lambda expressions as anonymous methods—methods without a name.

Find the following examples which shows the use cases of lambda expressions.

  • In the following example defining a Lambda Expression corresponding to the functional interface IntegerOperation. At the same time anonymous inner class could be used for implementation of the functional interface. But using the Lambda expression makes the code more compact and easy.

    	public class LambdaExpressionOperationTest {
    	  
    		/*Functional Interface which contains an abstract method operation with two parameters*/ 
    	    interface IntegerOperation {
    	        int operation(int a, int b);   
    	    }
    	  
    	    /*A method which receives two parameters along with a functional interface reference which 
    	    in turn perform some operation*/
    	    public int performBinaryOperation(int a, int b, IntegerOperation iop) {
    	        return iop.operation(a, b);
    	    }
    	 
    	    public static void main(String... args) {
    	    
    	        LambdaExpressionOperationTest obj = new LambdaExpressionOperationTest();
    	        
    	        /*Instance of IntegerOperation which contains a Lambda Expression*/
    	        IntegerOperation addition = (a, b) -> a + b;
    	        
    	        /*Instance of IntegerOperation which contains a Lambda Expression*/
    	        IntegerOperation subtraction = (a, b) -> a - b;
    	        
    	        System.out.println("2 + 2 = " +
    	        		obj.performBinaryOperation(2, 2, addition));
    	        System.out.println("12 - 2 = " +
    	        		obj.performBinaryOperation(12, 2, subtraction));    
    	    }
    	}
    	
    	Output :
    	
    	2 + 2 = 4
    	12 - 2 = 10
    	
  • Using the Lambda Expression for the forEach() method provided in the collections classes in the Java8 version. Passing functional interface instance defined using Lambda Expressions.
    	package LambdaExpression;
    	
    	import java.util.HashSet;
    	
    	public class LambdaTestInterator {
    	
    		
    		
    		public static void main(String[] args) {
    		
    			HashSet intSet=new HashSet<>();
    			
    			intSet.add(1);
    			intSet.add(2);
    			intSet.add(3);
    	
    			/*Passing a Lambda Expression for Consumer functional 
    			interface defined in java.util.function package. As forEach method defined for 
    			collection classes receives the Consumer reference, a lambda expression is
    			being passed for the Consumer functional interface
            	
    			
    			/*Without any parenthesis and curly braces*/
    			intSet.forEach(intVal -> System.out.println(intVal));
    			
    			/*Passign the variable with parenthesis*/
    			intSet.forEach((intVal) -> System.out.println(intVal));
    			
    			/*Passing parameter with parenthesis and expression with curly braces*/
    			intSet.forEach((intVal) -> {
    										System.out.println(intVal);
    									   });
    			
    			
    		}
    		
    		
    	}