What Are Lambda Expressions?
This is an introductory lesson on lambda expressions. You will learn about the lambda operator, expression, syntaxes and more!
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
- Provides the implementation of a functional interface.
- 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.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.