Lambda Expressions

Lambda expression is a new feature which is introduced in Java 8. A lambda expression is an anonymous function. A function that doesn’t have a name and doesn’t belong to any class.

Java Lambda Expression Syntax

To create a lambda expression, we specify input parameters (if there are any) on the left side of the lambda operator ->, and place the expression or block of statements on the right side of lambda operator.
Example
The lambda expression (x, y) -> x + y specifies that lambda expression takes two arguments x and y and returns the sum of these.

                    
                      (parameters) -> expression
                      (x, y) -> x + y
                    
                  
Lambda expression vs method in Java

A method (or function) in Java has 4 main parts:

  1. 1. Name
  2. 2. Parameter list
  3. 3. Body
  4. 4. return type

A Lamdba expressions in Java has only body and parameters list parts:

  1. 1. No name – function is anonymous so we don’t care about the name
  2. 2. Parameter list
  3. 3. Body – This is the main part of the function.
  4. 4. No return type – The java 8 compiler is able to infer the return type by checking the code. you need not to mention it explicitly.

Where to use lambda functions in java

To use lambda expression, you need to either create your own functional interface or use the pre defined functional interface provided by Java. An interface with only single abstract method is called functional interface(or Single Abstract method interface), for example: Runnable, callable, ActionListener etc.

Features of Lambda Expressions

A lambda expression can have zero, one or more parameters.

                    
                      (x, y) -> x + y
                      (x, y, z) -> x + y + z
                    
                  

The body of the lambda expressions can contain zero, one or more statements. If the body of lambda expression has a single statement curly brackets are not mandatory. When there is more than one statement in the body then these must be enclosed in curly brackets.

                    
                      (parameters) -> { statements; }
                    
                  

  1. - The type of the parameters can be explicitly declared or it can be inferred from the context. In previous example, the type of addOperation and appendOperation is derived from context.
  2. - Multiple parameters are enclosed in mandatory parentheses and separated by commas. Empty parentheses are used to represent an empty set of parameters.

                    
                      () -> expression
                    
                  

  1. - When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses.

                    
                      a -> return a * a;
                    
                  

  1. - A lambda expression cannot have a throws clause. It is inferred from the context of its use and its body.
  2. - Lambda expressions cannot be generic i.e. they cannot declare type parameters.

Using lambda expression to iterate over a List and perform some action on list items

In the given example, we are iterating over the list and printing all the list elements in the standard output. We can perform any desired operation in place of printing them.

                    
                      List<String> pointList = new ArrayList();
 
                        pointList.add("1");
                        pointList.add("2");
                         
                        pointList.forEach( p ->  { System.out.println(p); } );