Java introduced lambda expressions to make code simpler and shorter, especially when working with collections and functional-style operations.
In this chapter, you will learn about lambda expressions, functional interfaces, and how they help in writing clean and efficient Java code.
A lambda expression is a feature introduced in Java 8 that provides a short and clear way to represent the implementation of a functional interface (an interface with a single abstract method).
It is widely used in the collection framework to perform operations like iteration, filtering, and data processing.
With lambda expressions, there is no need to write separate classes or methods for implementation. Instead, you can write the implementation directly using an expression.
A functional interface is an interface that contains only one abstract method. Lambda expressions are used to provide the implementation of such interfaces.
Java provides the @FunctionalInterface annotation to declare a functional interface.
A method reference is a shorter way to refer to a method using :: operator. It is used in place of lambda expressions when the method already exists.
Here is the syntax for function reference:
Example:
Here, instead of writing a lambda expression, we directly refer to the println method.
Lambda expressions make Java code shorter, cleaner, and easier to read. They remove the need for extra boilerplate code, especially when implementing functional interfaces.
They are very useful when working with collections and streams to write operations like filtering and iteration in a simple way.
Lambdas also replace anonymous classes which makes the code less complex and to write more structured and less error-prone programs.
To create a lambda expression, define the parameters inside parentheses {}, use the -> arrow, and write the implementation (body) on the right side.
A lambda expression follows a simple syntax:
A lambda expression consists of three main parts:
Here is the syntax of a lambda expression with no parameter:
Here is the syntax of a lambda expression with only one parameter:
Here is the syntax of a lambda expression with two parameters:
Before lambda expressions, we used anonymous classes to provide implementation of interfaces.
In this example, we implement an interface using an anonymous class without using a lambda expression.
Output:
Drawing 10
Lambda expressions provide a shorter and cleaner way to implement functional interfaces. They remove the need for anonymous classes.
In this example, we use a lambda expression to simplify the implementation of a functional interface.
Output:
1 2 3 4 5 6 7 8 9 10
A lambda expression can have no parameters when the method does not require any input.
In this example, we use a lambda expression that does not take any parameters.
Output:
I have nothing to say.
A lambda expression can take a single parameter. Parentheses are optional when only one parameter is used.
In this example, we use a lambda expression with a single parameter to process input.
Output:
Hello, Sonoo Hello, Sonoo
Lambda expressions can accept multiple parameters when the method requires more than one input.
In this example, we use a lambda expression with multiple parameters to perform a calculation.
Output:
Average: 20.0
Lambda expressions may or may not use the return keyword. It is optional for single-line expressions but required for multiple statements.
In this example, we demonstrate lambda expressions with and without using the return keyword.
Output:
30 300
Lambda expressions are commonly used with collections to iterate elements in a simple way.
In this example, we use a lambda expression with forEach to iterate over a collection.
Output:
ankit mayank irfan jai
Lambda expressions can be used to simplify thread creation by implementing the Runnable interface.
In this example, we use a lambda expression to create and run a thread.
Output:
Thread1 is running... Thread2 is running...
We request you to subscribe our newsletter for upcoming updates.