Skip to content

What Are Lambda Expressions?

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

Gopi Gorantala
Gopi Gorantala
2 min read

Table of Contents

Lambdas were introduced in Java 8, which uses both new packages introduced from Java 8.

1. java.util.function - Function

2. java.util.stream - Stream

Lambda operator

The Lambda operator "->" introduces a Lambda expression.

The lambda expression syntax uses the lambda operator, shown below syntaxes.

value -> value * 2;
value -> { 
   return value * 2; 
}

What is a lambda expression?

A lambda expression defines an anonymous function, or more correctly, an instance of an anonymous class that implements a functional interface.

Lambda expressions provide a clear and concise way of implementing a single-method interface using an expression. They help to iterate, filter, and extract data from the collection.

A lambda expression has the following syntax:

(parameters) -> expression

Here, parameters refers to the function's input parameters and expression refers to the function's operation. For example, consider the following lambda expression:

(x, y) -> x + y

This lambda expression takes two input parameters x and y, and returns their sum.

Lambda expressions can be used in various situations in Java, such as with functional interfaces, which contain a single abstract method. Using lambda expressions, you can pass behavior as a parameter to a method or store it in a variable.

For example, consider the following code:

import java.util.Arrays;
import java.util.List;

public class PrintElements {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    numbers.forEach((Integer value) -> System.out.println(value));
  }
}

Here, we create a list of numbers and use the forEach method to iterate over the list. We pass a lambda expression to the forEach method to print each value in the list.

Lambda expressions can also be used with streams to perform operations such as filtering and mapping on collections. Overall, lambda expressions are a powerful feature in Java that enable functional programming and make code more concise and expressive.

What Are The Uses Of Lambda Expressions?

There are quite a few uses/advantages. Out of them, the most popular ones are

  1. Provides the implementation of a functional interface.
  2. Less code.

Syntax

There are 3 ways we could re-write the syntax as follows.

No Parameter

When there is no parameter for the lambda expressions, we use empty parenthesis to represent the lambda expression.

() -> {
   // statements 
}

One Parameter

We can remove the parenthesis when there is a single parameter in the lambda input. The following are valid syntaxes for a single parameter lambda expression.

// All of the below single parameter lambda expressions are valid.

// first way
(ParameterType parameter1) -> {
   // single parameter statements 
}

// second way
(parameter1) -> {
   // single parameter statements 
}

// third way
parameter1 -> {
   // single parameter statements 
}

More Than One Parameter

When more than one parameter is declared in the lambda input parenthesis, all of the following syntaxes are valid.

// Both of the below single parameter lambda expressions are valid.

// first way
(ParameterType parameter1, ParameterType parameter2) -> {
   // multiple parameter statements 
}


// Another way
(parameter1, parameter2) -> {
   // multiple parameter statements 
}

This concludes the introduction of lambda expressions. In the next lessons, you will learn more about writing and using them.

Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.