Java lambda and functional programming

Java lambda expressions are syntactical sugars, It is a way of reducing lines of coded.  In other words, trim down the code and increase readability. In this blog, we will be discussing Java lambda and functional programming.

AddInt addFunc = (a,b) -> a+b;
1                  2       3

Lambda functions are used with functional interfaces in Java. There three important rules to write lambda.

The class/interface (1) should only one abstract method. The argument for the abstract method should match to lambda before arrow part(2). The return type(3) should match to abstract method return type.

Lambda expression.

Zero parameters.

Zero parameters is just an empty parenthesis. The function’s name and object doesn’t need to be the same, it can be different.

interface FindRandom{ 
    int random();

FindRandom randomFunc = () -> Maths.random();

randomFunc.random() // return a random number

One parameter.

One parameter do not require parenthesis. There is a Java provided a standard interface for these kinds of interface, Which accepts and returns only one argument.  Please check Functional<T,R> interface. With this standard interface, we don’t need to create any new interfaces anytime for any methods which accept and return only one variable.

interface PrintSquare{ 
    int square(int a);

PrintSquare squareFunc = a -> a*a;

squareFunc.square(3);  // return 9

Multiple parameters.

Multiple parameters are enclosed in parenthesis.  Function interface having method which accepts two parameters and returns one value are often called Two-Arity Functions. Here are the Java-defined interfaces for these kinds of methods.
ToIntBiFunction, ToDoubleBiFunction, ToIntBiFunction, and ToLongBiFunction

interface AddInt{ 
    int add(int a, int b);

AddInt add = (a,b) -> a+b;

add.add(4,5); // return 9

Multiple lines of code in functions body.

We add curly braces in case there are multiple lines in the body. It properly defines the starting and ending of the block.

interface SquareAndAdd{ 
    int squareAdd(int a, int b);

SquareAndAdd func = (a,b) -> {
    a = a*a;
    b = b*b;
    return a+b;

func.squareAdd(2,3); // returns 13

Pass Lambda function as a variable.

In Javascript we have can pass callback function to a function as below.

var callback = function(int a){

var mainFunc = function(var i, var j, var func){
   var k = i +j;

mainFunc(2,4,callback); // Here callback is passed as parameter and will be execute in form inside mainFunc

With the functional interface and lambda expression in Java8 we can do the same.

interface DoSquare{ 
  int square(int a);

interface AddIntThenSquare{ 
  int add(int a, int b, DoSquare doSqaure);

DoSquare squareFunc = a -> a*a;
AddIntThenSquare addFunc = (a,b,c) -> {
    int k = a+b;
    return c.square(k); 

System.out.println(addFunc.add(2, 3, squareFunc)); //Prints 25

Return Lambda function from the function

In the below example, we have a function which takes a string parameter and returns a method(functional interface).

interface Calculator{  
  OperationWrapper getOperationWrapper(String type);

interface OperationWrapper{
  int operate(int a, int b);

Calculator c = type -> {
    case "add": 
      return (a,b) -> a+b;
    case "substract": 
      return (a,b) -> a-b;
    case "multiply": 
      return (a,b) -> a*b;
      return (a,b) -> a/b;
System.out.println(c.getOperationWrapper("add").operate(1, 2)); //prints 3
System.out.println(c.getOperationWrapper("substract").operate(1, 2)); //prints -1
System.out.println(c.getOperationWrapper("mulitply").operate(1, 2)); //prints 2
System.out.println(c.getOperationWrapper("devide").operate(1, 2)); //prints 0

There is a little more to lambda functions, Double colon operator. It looks something like this.

It might look a little bit confusing but it fairly simple and easy to understand. This is it on our Java lambda and functional programming - Part 2. Please check out next blog in this series Java 8 double colon operator.