Lambda Expression in Java 8

|
| By Webner

Lambda expressions are introduced in Java 8 and are a very interesting feature of Java 8. A lambda expression is a short block of code that takes in parameters and returns a value. Lambda expressions are similar to methods, but they do not need a name and they can be implemented right in the body of a method.

A lambda expression is an anonymous function. It doesn’t belong to any class.

It provides a clear and concise way to represent one method interface using an expression. It is very useful in the collection library. It helps to iterate, filter, and extract data from the collection.

A Java lambda expression can be passed around as if it was an object and executed on demand. Java lambda expressions are commonly used to implement simple event listeners/callbacks, or in functional programming with the Java Streams API.

Syntax

The simplest lambda expression contains a single parameter and an expression:

parameter -> expression body

To use more than one parameter :

(parameter1, parameter2) -> expression

(parameter1, parameter2) -> { code block }

To create it, we specify input parameters (if there are any) on the left side of the lambda operator ->, and place the expression or block of statements on the right side of the lambda operator. For example, the lambda expression (x, y) -> x + y specifies that lambda expression takes two arguments x and y, and returns the sum of these.

Expressions are limited. They have to immediately return a value, and they cannot contain variables, assignments, or statements such as if or for. In order to do more complex operations, a code block can be used with curly braces. If the expression needs to return a value, then the code block should have a return statement.

Example

() -> System.out.println(“Hello”)

This example identifies a lambda for outputting a message to the standard output stream. From left to right, () identifies the lambda’s formal parameter list (there are no parameters in the example), -> indicates that the expression is a lambda, and System.out.println(“Hello”) is the code to be executed.

Lambda expression vs method in Java

A method (or function) in Java has these main parts:

  1. Name
  2. Parameter list
  3. Body
  4. return type.

A lambda expression in Java has these main parts:
A Lambda expression only has a body and parameter list.

  1. No name – function is anonymous so we don’t care about the name
  2. Parameter list
  3. Body – This is the main part of the function.
  4. No return type – The java 8 compiler is able to infer the return type by checking the code. you need not mention it explicitly.

Important characteristics of a lambda expression

  • 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 parentheses around parameters − 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.

Higher Efficiency − By using Stream API and lambda expressions, we can achieve higher efficiency (parallel execution) in the case of bulk operations on collections.

Leave a Reply

Your email address will not be published. Required fields are marked *