Lambda expression in java 8

In this article, we will understand what is a functional interface and lambda expression in java 8 with proper examples.

Functional Interface

It is an interface with one abstract method ( a method that doesn’t have any implementation ). It can also have default and static methods, we will try to cover default and static methods in a separate article. As of now, we will focus on an abstract method which is enough to understand a lambda expression in java.

public interface Operations {
    int operate(int operand1, int operand2);
}

We can add @FunctionalInterface annotation to a functional interface.

@FunctionalInterface
public interface Operations {
    int operate(int operand1, int operand2);
}

If we are adding @FunctionalInterface annotation, it means the interface is strictly functional. If we are adding more than one abstract method to the functional interface it will give a compile-time error.

@FunctionalInterface
public interface Operations {
    int addition(int operand1, int operand2);
    int subtraction(int operand1, int operand2);
}

CE : Invalid '@FunctionalInterface' annotation ; Operations is not a functional interface .

Lambda expression

Lambda expression is equivalent to a method, but this method will be nameless and the return type is optional. We can say lambdas are the basic unit of Functional Programming.

The lambda expression in java looks like : ( parameter ) -> expression

As Java is having a lot of boilerplate code hence engineers tried to reduce some unwanted code. Consider the example below :

public interface Operations {
  int operate(int operand1, int operand2);
}

Now we will implement the above interface using an anonymous class :

public class MainProgram {
  public static void main(String[] args) {
    Operations operations = new Operations() {
      @Override
      public int operate(int operand1, int operand2) {
        return operand1 + operand2;
      }
    };
    System.out.println("Result : " + operations.operate(5, 10));
  }
}

Output : Result : 15

When implementing the above interface there is some boilerplate code. Hence the lambda expression helps to remove such extra code. Now we can focus on the business logic, instead of focusing on the code which is of no importance.

public class MainProgram {
  public static void main(String[] args) {
    Operations operations =
      (operand1, operand2) -> {
        return (operand1 + operand2);
      };
    System.out.println(operations.operate(5, 10));
  }
}

The above lambda expression is of form : ( parameter1, parameter2 ) -> expression

public int operate(int operand1, int operand2) {
    return operand1 + operand2;
}
   
   is equivalent to

( operand1, operand2 ) -> { return ( operand1 + operand2 ) }

Why functional interface is needed?

In the above example, we have removed the method name as well as data types and we have focused only on our business logic. Writing a lambda expression is possible because of the functional interface. As a functional interface is having only one method hence JVM knows it is that single method that it has to implement hence no ambiguity. Let us try an example where two abstract methods are part of an interface.

Note * As we are using two methods within an interface so it will not be a functional interface anymore.

public interface Operations {
  int addition(int operand1, int operand2);
  int subtraction(int operand1, int operand2);
}

public class MainProgram {
  public static void main(String[] args) {
    Operations operations =
      (firstOperand, secondOperand) -> {
        return (firstOperand + secondOperand);
      };
    System.out.println(operations.addition(5, 10));
  }
}

Compilation Error : The target type of this expression must be a functional interface

JVM will get confused and will not understand it has to provide the implementation for which method in the interface ( as there are 2 methods now, addition and subtraction ) and there will be ambiguity. So java will not compile such code and throw a compile-time error. Hence lambda expression can only be used with a functional interface.

In this article, we have covered what is a functional interface and lambda expression in java 8 with proper examples. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send an email.

Leave a Comment