CORE JAVA

Lambda Expression in Java

Published on

A Lambda Expression in Java is a feature introduced in Java 8. It helps developer to write clean , concise & flexible code, particularly when dealing with functional interfaces. It enable us to use functionality as a method argument. Lambda expressions enable you to create anonymous functions(functions without a name) which can be passed around the code as a variables.

Lambda expression is already used in many languages before like LISP, C++, C#, SCALA, RUBY etc & lately introduced in java .

The main objective of Lambda Expression is –

1: To Enable Functional Programming Language in Java.

2: To write clean , concise & flexible code.

3: To use API very easily and effectively.

4: To Enable parallel processing.

Lambda Expression is an anonymous function not having name, return type and modifier.

Lambda expression is a function which does not have any have modifier , return type & mehod name . Let us  try to write and  convert a normal function to equivalante lambda expression.

Convert Normal Function to Lambda Expression (without argument and no return type)
				
					 // Normal function without parameter
 public void testLambda(){

        System.out.println("I am Lambda");
    }

       // Lambda function
    ()-> {System.out.println("I am Lambda");}
				
			

Convert Normal Function to Lambda Expression (with argument and no return type)

				
					// Normal Function with argument
    public void testLambda( int a , int b){

        System.out.println(a+b);
    }

    // equivalent Lambda wxpression with argument
    (int a, int b)->{
        System.out.println(a+b);
    }

				
			

Convert Normal Function to Lambda Expression (with argument and return type)

				
					 // Normal Function with argument & return type
    public int testLambda( int a , int b){

        System.out.println(a+b);
    }

    // equivalent Lambda function with argument & return type
    (int a, int b)->{
       
        return a*b;
    }

				
			

If Lambda expression has only one statement , we can ignore curly braces. 

				
					 (a ,b)-> System.out.println("I am Lambda");
				
			

Sometimes compiler can guess the datatype based on the context then we can remove data type  as well  & if there is only one statement we remove curly brces & return keyword too. If there is only argument we can remove parenthesis as well . so the most simplified version of lambda express is-

				
					 public int nameLength( String name){

        return name.length();
    }

    // Lambda Expression
    s-> s.length();

				
			

Lets Summarize the Properties Of Lambda Expression below:

1: A lambda expression can take any number of Parameter

()-> System.out.println(“Java”);     // Zero Parameter

(s)->s.length(); // One Parameter

(a+b)-> System.out.println(a+b);  // Two Parameter

2: if there is only one parameter, we can ignore the parenthesis

s->s.length();

3: We can ignore the type of parameter if compiler can expect the type based on content then we can remove the type as well. This is called “Type Inference “.

(a,b)->System.out.println(a+b);

4: If lambda expression containes more than one statement then all the statements must enclose within curly braces;

()->{

Statement 1;

Statement 1;

      Statement 1;

}

5: If Lambda Expression return something, we can ignore ‘return’ keyword also

s->s.length();

 

 

1 Comment

Popular Posts

Copyright © 2024 javadsa.com