Functional Programming in Java: 2. Lambdas

Vivek T S
3 min readMar 28, 2019

--

Photo by Peter Nguyen on Unsplash

The addition of lambda expressions paved the way for a new operator in the Java operator library. It’s the lambda operator (or arrow operator).

Lambda operator — > which means "goes to" or “becomes”

A lambda expression can be segregated into two parts, left side parameter list, and right side expression logic with lambda operator dividing them. There can also lambda expressions without any parameter(s). In those cases, empty parenthesis must be included on the left side and the right side representing the processing logic involved. Below is an example of an empty parameter lambda expression,

1. () -> 3.14

It means the expression takes no parameter and it returns a double value. It is equivalent to the method form written below,

double getPiValue(){
return 3.14;
}

Here’s an example of a lambda expression that takes a single input parameter to return the square of the input,

2. (n) -> n*n

And it’s method equivalent would be,

int getSquareValue(int n){
return n*n;
}

Look how the method equivalent specifies the data type everywhere. In the lambda expression, the datatype of n is automatically inferred by the compiler.

But always remember, a lambda expression can only be assigned (or substituted) to a Functional Interface and not ordinary primitive or reference types. So, the compiler automatically infers the type from its functional interface declaration.

double piValue = () -> 3.14; //Won't compile

Also, one could notice from the examples 1 and 2 that the expression doesn’t contain any return statement even though they seem to return values. These expressions are called Single Lambda Expressions where the expression ends with a single statement. Though these expressions return values, return is optional. A few more examples,

3. (n,k) -> Math.pow(n,k)    double getPowerValue(double n, double k){
return Math.pow(n,k);
}
4. (n) -> System.out.println(n) void print(int n){
System.out.println(n);
}

Block Lambda Expressions

Imagine defining a method called getFactorial which returns the factorial of a given number using a for loop,

int getFactorial(int n){
int fact = 1;
for(int i=n;i>=1;i--)
fact*=i;
return fact;
}

The above method gets an int and returns an int. The same can be achieved using a lambda expression as below,

(n) -> { 
int fact = 1;
for(int i=n;i>=1;i--)
fact*=i;
return fact;
}

Don’t read too much into the above expression as it is incomplete without defining an associated Functional Interface. But notice how similar/different it looks than the original method equivalent. Since this expression takes more than one step to execute the logic, it is enclosed inside {} braces. Also, a return must be explicitly mentioned. These are called Block Lambda Expressions.

Let’s revisit the above expressions on how they combine with functional interfaces to achieve its purpose in future segments.

…to be continued.

Thank you for reading. Let me know your thoughts.

Also, check part one of the series here, Functional Programming in Java: 1. Introduction

--

--