Lambda expressions in Java are a shorthand way of representing anonymous functions: functions without a name that can be executed later by passing them around. They are essentially a way of defining a method without formally declaring it in a class.
The syntax for a lambda expression
The simple lambda expressions contain a single parameter and expression,
parameter -> expression
If you need to use more than one parameter, wrap them into a parentheses ().
(parameter1, parameter2) -> expression
Here is a simple example where a lambda expression is used to define a function that calculates the square of a number,
Function without lambda expression
public class Main {
public static void main(String[] args) {
System.out.println(myFuntion(5, 4));
}
public static int myFuntion(int a, int b) {
return a * b;
};
}
Function with lambda expression
import java.util.ArrayList;
class Program {
public static void main(String args[])
{
// Creating an ArrayList with elements {1, 2, 3, 4, 5, 6, 7}
ArrayList<Integer> arrList = new ArrayList<Integer>();
arrList.add(1);
arrList.add(2);
arrList.add(3);
arrList.add(4);
arrList.add(5);
arrList.add(6);
arrList.add(7);
// Using lambda expression to print all elements
System.out.println("All elements");
arrList.forEach(n -> System.out.print(n+ "\t"));
// Using lambda expression to print even elements
System.out.println("\nList of Even Numbers");
arrList.forEach(n -> {
if (n % 2 == 0)
System.out.print(n+ "\t");
});
// Using lambda expression to print odd elements
System.out.println("\nList of Odd Numbers");
arrList.forEach(n -> {
if(n%2 != 0)
System.out.print(n+ "\t");
});
}
}
Output-
All elements
1 2 3 4 5 6 7
List of Even Numbers
2 4 6
List of Odd Numbers
1 3 5 7
Usage
Lambda expressions are mainly used in functional networks, which are exactly one-way abstract interfaces. They provide a concise way to implement a method defined by a user interface.
For example, in Java, the java.util.function package contains many function interfaces such as
Predicate, Consumer, and Function, which can be effectively implemented in lambda expressions.
Benefits
Conciseness- Lambda expressions reduce the amount of boilerplate code that comes with anonymous classes. Readability- The code is made readable by focusing on what needs to be done rather than how. Functional Programming- Lambda expressions enable a highly efficient functional form in Java, facilitating tasks such as mapping, filtering, and reducing collections.
Limitations
There are some restrictions on lambda expressions:
Can only be used in functional interfaces. Cannot have return statements without curly braces {} around the body. Cannot contain void as a return type (use Consumer for the void method).
When to Use
Lambda expressions are especially useful in situations where you need to pass a statement (to some extent) as an argument to a method or do it asynchronously. It is widely used in streams, threads, event handling, and other functional programming constructs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Lambda Expressions in Java
Lambda expressions in Java are a shorthand way of representing anonymous functions: functions without a name that can be executed later by passing them around. They are essentially a way of defining a method without formally declaring it in a class.
The syntax for a lambda expression
The simple lambda expressions contain a single parameter and expression,
If you need to use more than one parameter, wrap them into a parentheses ().
Here is a simple example where a lambda expression is used to define a function that calculates the square of a number,
Function without lambda expression
Function with lambda expression
Output-
Usage
Lambda expressions are mainly used in functional networks, which are exactly one-way abstract interfaces. They provide a concise way to implement a method defined by a user interface.
For example, in Java, the
java.util.functionpackage contains many function interfaces such asPredicate,Consumer, andFunction, which can be effectively implemented in lambda expressions.Benefits
Conciseness- Lambda expressions reduce the amount of boilerplate code that comes with anonymous classes.
Readability- The code is made readable by focusing on what needs to be done rather than how.
Functional Programming- Lambda expressions enable a highly efficient functional form in Java, facilitating tasks such as mapping, filtering, and reducing collections.
Limitations
There are some restrictions on lambda expressions:
Can only be used in functional interfaces.
Cannot have
returnstatements without curly braces {} around the body.Cannot contain
voidas a return type (useConsumerfor the void method).When to Use
Lambda expressions are especially useful in situations where you need to pass a statement (to some extent) as an argument to a method or do it asynchronously. It is widely used in streams, threads, event handling, and other functional programming constructs.
Also, Read: What are lambda expressions, and how are they used in LINQ queries?