Java8 lambda expression

In this article, we will discuss one of the important feature introduced in Java 1.8 version i.e. Lambda Expression.
Java 8 introduced lambda expression to move toward functional programming. A lambda expression is an anonymous function that doesn’t have a name and doesn’t belong to any class. They can be passed as a parameter to another function

Lambda expressions are the way to implement functional programming in the object-oriented world of Java. It has been added with the Java 8 release.

Lambda Introduction

Lambda expressions can only implement functional interfaces, which is an interface with only one abstract method. The lambda expression essentially provides the body for the abstract method within the functional interface.

To use a lambda expression, you can either create your own functional interface or use the predefined SAM/functional interface provided by Java API.

Before Java 8:  Runnable, Callable, Comparable, Comparator which contains exactly one method like run(), call(), compareTo(), compare() respectively
Post Java 8: Java designers have created many functional interfaces under the java.util.function package with Java 8, suitable for common use-case scenarios.

Advantages of Lambda Expressions

  • Enables functional programming.
  • Makes the code more readable.
  • Allows getting rid of boilerplate codes.
  • Facilitates the use of APIs and libraries.
  • Supports parallel programming.

What is Lambda Expression

Lambda  expression is simply the function without any name, the simplest lambda we can denote as

()->{}

There are three Lambda Expression Parameters are mentioned below:

  • Zero Parameter
  • Single Parameter
  • Multiple Parameters
  1.  Lambda with Zero parameter
    ()->{
    //write some statemnet here
    }
  2. Lambda with Single parameter
    (arg1)->{
    //write some statemnet here
    }
  3. Lambda with Multiple parameter
    (arg1,arg2)->{
    //write some statemnet here
    }

How to convert method into Lambda

how can we convert Java methods into Lambda Expression which is more concise means less code. A normal method in Java contains following things,

  • access modifier
  • return type
  • method name
  • argument lists
  • method body

Java lambda expression consists of three components.

  1. Argument-list: It can be empty or non-empty as well.
  2. Arrow-token: It is used to link arguments-list and body of expression.
  3. Body: It contains expressions and statements for the lambda expression.

Structure of Lambda Expressions

Lambda expressions can be assigned to a variable. In other words, it can be thought of as assigning a method to a variable.

greetFunction = public void perform() {
    System.out.println("Hello world!");
};

Above, a representative structure is shown. The transition from this structure to a lambda expression structure is as follows:

  • It makes sense to use an access modifier when creating a method that belongs to a class, but since lambda expressions are not related to a class, an access modifier is not needed.
  • There is no need to specify a method name, as the function provided by a lambda expression will be used through a variable.
  • Since the compiler knows the return type of the written lambda expression -it is specified by us, I’ll mention-, there is no need to specify a return type.
  • As in the above clause, there is no need to specify the types of parameters, since the compiler knows the types of parameters.
  • If a lambda expression is a single line, the curly braces are optional.

Important points to remember while writing Lambda Expression

  • To call Lambda Expression, Functional interface is must which consists of single abstract method
  • Curly braces is optional if it consists of single Java statement. otherwise curly braces is must for more than 2 statements.
  • Similarly, parenthesis is optional if it consists of single input arguments; otherwise it is must for all other cases even if it doesn’t accepts any input arguments like empty parenthesis ( )
  • Specifying access-specifier for input arguments is optional, as it can be inferred from the value passed to lambda expression
  • While returning value, return keyword is optional if we are not using curly braces and it consists of single Java statement; otherwise it is must for all other case

2 thoughts on “Java8 lambda expression”

Leave a Comment