Skip to content

How To Write Lambda Expressions

This lesson introduces the basic lambda expression structure with tips to write efficient code.

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

First, let us look at the basic structure of lambda expression. This could help us understand writing lambdas easily.

Basic Structure

The basic structure of lambda expression is:

Basic steps

Java Lambda expression contains the following parts:

Parameters-list: This can be empty or contain one or more parameters, separated by commas. If there is only one parameter, you can omit the parentheses.

  1. Lambda operator: This separates the parameter list from the lambda expression's body or, in simple terms, connects the argument list with the lambda expression's body.
  2. Expression: Finish with the expression or statement(s) to be executed: expression or { statements }.


Here is an example of a lambda expression that takes two integers as input and returns their sum:

(int a, int b) -> a + b

This lambda expression takes two integer parameters a and b and returns their sum. Note that the parameter types are specified in parentheses, and the return type is inferred based on the expression.

() -> System.out.println("Hello, world!")

This lambda expression has an empty parameter list, and the body contains a single statement to print a message to the console.

Lambda expressions can be assigned to functional interfaces or used directly as arguments to methods. For example, to use the above lambda expression with the Runnable functional interface, you can write:

Runnable helloWorld = () -> System.out.println("Hello, world!");

This creates a Runnable object that executes the lambda expression when its run method is called.

Tips when writing lambda expressions

Parameter lists sometimes have zero or more parameters.

There are rules to follow when writing a lambda expression. We don’t need to follow all, but to improve code quality we have to follow these tips when writing a lambda expression.

  1. When there are no parameters, we should explicitly give ().

If there is a single parameter, we can ignore the parenthesis below. Both syntaxes are valid.

  • (message) -> {}
  • message -> {}
  • When the body is a single line, we can exclude the curly braces {, } and return. The following example explains it.
// Both of the below expressions are valid.

// 01: Lambda expression printing Hello on console
  () -> {

// 02: Lambda expression printing Hello on console
  () -> System.out.println("Hello");
  • Java understands the underlined functional interface when running through the lambda expression. Because of this, we don’t need to have the parameter typed inside the parameters list.
// All of the below syntaxes print same output - Hello, fullName
(String fullName) -> {
    System.out.println("Hello, " + fullName);

// Above can be refactored as follows.
(fullName) -> {
    System.out.println("Hello, " + fullName);

// further more, we can replace it with
fullName -> System.out.println("Hello, " + fullName);

Gopi Gorantala Twitter

Gopi is a highly experienced Full Stack developer with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.


Related Posts

Members Public

What Are Lambda Expressions?

This is an introductory lesson on lambda expressions. You will learn about the lambda operator, expression, syntaxes and more!

Members Public

Power Of Two (Exercise Problem)

This is an exercise problem for your practice. Try to come up with an approach and solve it by yourself. Good Luck!

Members Public

User-Defined Customer Interface (Exercise Problem)

This is an exercise problem for your practice. Try to come up with an approach and solve it by yourself. Good Luck!