Lambda Expressions in Java

Lambda Expressions in Java

A functional interface, introduced in Java 8, is an interface which has only a single abstract method. Conversely, if you have any interface which has only a single abstract method, then that will effectively be a functional interface. This interface can then be used anywhere where a functional interface is eligible to be used.
One of the most important uses of Functional Interfaces is that implementations of their abstract method can be passed around as lambda expressions. By virtue of their ability to pass around functionality(i.e. behavior), functional interfaces primarily enable behavior parameterization.
Lambda Expressions are Java’s answer to the functional programming concept of closures in other languages. A Lambda Expression or lambda  is a representation of an anonymous function which can be passed around as a parameter thus achieving behavior parameterization. Lambda is very similer to function but without function name i.e. anonymous. A lambda consists of a list of parameters, a body, a return type and a list of exceptions which can be thrown.
An instance of a lambda can be assigned to any functional interface whose single abstract method’s definition matches the definition of the lambda. Since the definition of a lambda can match the definition of multiple functional interfaces, hence, a lambda instance can be assigned to any of these matching interfaces.

import java.util.*;
public interface MyFunctionalInterface {
abstract double functional(Vector <Double> nos);

import java.util.*;
public class MyLambdaExpression {
public static void main(String args[]){
MyFunctionalInterface fun = (nos)-> {double sum = 0; for(Double n :nos)sum+= n; return sum;};
Vector<Double> nos = new Vector<Double>();
System.out.println("Sum:" + fun.functional(nos));
fun = (doubleNos)-> {double sum = 0;  for(Double n :doubleNos)sum+= n; return sum/doubleNos.size();};
System.out.println("Average:" + fun.functional(nos));

Lambda Characteristics.

  • Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
  • Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
  • Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
  • Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

Important points to understand regarding the lambda expression

The element to the left of the arrow(->) are the parameters of the lambda.
To the right of the arrow(->) we have the body of the lambda. Body is where the actual processing within a lambda happens. i.e. the logic of the lambda goes here.

Lambda Return Types

Lambda syntax contains 2 variants of return types. This types are based on the contents in lambda following the arrow(->) sign 
  • Variant 1.(parameters) -> expression– In this variant the return type of the lambda expression will be same as the resultant type of the expression
  • Variant 2.(parameters) -> {statements;} – In this variant, there will be no return type(or void return type) unless the statements inside the curly braces explicitly return something at the end. In that case the return type will be same as the type of the variable returned.