In this blog post, we'll take a detailed look at the syntax of lambda expressions in Java, along with examples to illustrate the various forms they can take.
A lambda expression is an anonymous function that can have parameters and a body. The body can contain zero, one, or more lines of code. lambda expressions can be assigned to variables whose type is a functional interface – an interface with a single abstract method.
Lambda Expression Syntax
Here is the general syntax for a lambda expression in Java:
(parameters) -> expression
Or
(parameters) -> { statements; }
Components of Lambda Expressions:
Parameters
The parameters section can contain zero, one, or more parameters. Their types may often be inferred by the compiler, allowing you to omit them:
Zero Parameters:
() -> System.out.println("No parameters!")
One Parameter (type inferred):
s -> System.out.println(s)
Multiple Parameters:
(int x, int y) -> x + y
Arrow Token
The arrow token (->) separates the parameters from the body of the lambda, pointing from the parameters to the action being performed.
Body
The body of the lambda expression can be a single expression or a block of code:
Single Expression:
a -> a * a (No need for the return keyword)
Code Block:
(a, b) -> { return a + b; } (Multiple statements inside braces)
Lambda Expression Syntax Examples
Lambda expressions in Java can be written in various forms, depending on the signature of the functional interface's abstract method they are implementing. Here are some different ways to write lambda expressions:
1. No Parameters, No Return Value
Lambda expressions can have no parameters and no return value.Runnable noParametersNoReturnValue = () -> System.out.println("Hello, World!");
2. Single Parameter, No Type Declaration
When there's only one parameter, you don't need to declare its type or use parentheses.
For example:
java.util.function.Consumer<String> singleParameterNoType = s -> System.out.println(s);
3. Single Parameter, Type Declaration
You can explicitly declare the type of the parameter:
java.util.function.Consumer<String> singleParameterWithType = (String s) -> System.out.println(s);
4. Multiple Parameters, Type Inferred
Java can infer the types of parameters, as shown below:
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersTypeInferred = (a, b) -> a + b;
5. Multiple Parameters, Type Declared
You can declare the types of all parameters:
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersWithType = (int a, int b) -> a + b;
6. Multiple Statements and Return Statement
For a lambda expression with multiple lines, you'll need curly braces and an explicit return statement:
java.util.function.BiFunction<Integer, Integer, Integer> multipleStatements = (int a, int b) -> {
int sum = a + b;
return sum;
};
7. Access to Local Variables
Lambda expressions can access local variables from the enclosing scope:
int factor = 2;
java.util.function.Function<Integer, Integer> accessLocalVariable = (int a) -> a * factor;
Lambda Expression Syntax - Cheat Sheet
Conclusion
Lambda expressions are a powerful feature in Java, enabling concise and readable code. By understanding the syntax and structure of lambda expressions, you can take advantage of this powerful feature in your own code.
Comments
Post a Comment
Leave Comment