Lambda expressions are one of the most impactful features introduced in Java 8. They enable functional programming in Java by allowing the creation of anonymous functions succinctly and elegantly. In this blog post, we'll explore various forms of writing lambda expressions.
Introduction to Lambda Expressions
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.
Various Forms of Writing Lambda Expression
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:
If the abstract method takes no parameters and returns no value, the lambda expression can be written with empty parentheses and a body:
() -> System.out.println("Hello, World!");
2. Single Parameter, No Type Declaration:
If the abstract method takes a single parameter, you can omit the parentheses around the parameter, and the type can be inferred:
s -> System.out.println(s);
3. Single Parameter, With Type Declaration:
You can also explicitly declare the type of the parameter:
(String s) -> System.out.println(s);
4. Multiple Parameters:
If the abstract method takes multiple parameters, you must include parentheses around the parameters. Types can be inferred or explicitly declared:
(a, b) -> a + b;
(int a, int b) -> a + b;
5. Return Statement:
If the body of the lambda consists of a single expression that returns a value, you can write it directly:
(int a, int b) -> a + b;
If the body consists of multiple statements, you must include braces and use a return statement:
(int a, int b) -> {
int sum = a + b;
return sum;
};
6. No Parameters, Return Value:
If the abstract method takes no parameters but returns a value, you can write it like this:
() -> 42;
7. Use Local Variables:
You can also access local variables from the enclosing scope within the lambda expression:
int factor = 2;
(int a) -> a * factor;
Example: Various Forms of Lambda Expressions
Here is a simple Java program that demonstrates various forms of Lambda expressions. I've included comments for each form, explaining how they're used, and print statements to display the output.
public class LambdaExamples {
public static void main(String[] args) {
// Lambda with no parameters and no return value
Runnable noParametersNoReturnValue = () -> System.out.println("Hello, World!");
noParametersNoReturnValue.run(); // Output: Hello, World!
// Lambda with a single parameter and no type declaration
java.util.function.Consumer<String> singleParameterNoType = s -> System.out.println(s);
singleParameterNoType.accept("Single Parameter, No Type!"); // Output: Single Parameter, No Type!
// Lambda with a single parameter and type declaration
java.util.function.Consumer<String> singleParameterWithType = (String s) -> System.out.println(s);
singleParameterWithType.accept("Single Parameter with Type!"); // Output: Single Parameter with Type!
// Lambda with multiple parameters, type inferred
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersTypeInferred = (a, b) -> a + b;
System.out.println(multipleParametersTypeInferred.apply(5, 10)); // Output: 15
// Lambda with multiple parameters, type declared
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersWithType = (Integer a, Integer b) -> a + b;
System.out.println(multipleParametersWithType.apply(5, 10)); // Output: 15
// Lambda with a return statement (multiple lines)
java.util.function.BiFunction<Integer, Integer, Integer> multipleStatements = (a, b) -> {
int sum = a + b;
return sum;
};
System.out.println(multipleStatements.apply(5, 10)); // Output: 15
// Lambda with no parameters and a return value
java.util.function.Supplier<Integer> noParametersReturnValue = () -> 42;
System.out.println(noParametersReturnValue.get()); // Output: 42
}
}
Output:
Hello, World!
Single Parameter, No Type!
Single Parameter with Type!
15
15
15
42
This program provides examples of different lambda expressions, including no-parameter, single-parameter, multiple-parameters, with or without type declarations, and various return types.
Conclusion
In summary, the form of a lambda expression can vary depending on the number and types of parameters, whether you include explicit type declarations, and whether the body consists of a single expression or multiple statements. The flexibility of Lambda expressions in Java allows you to write concise and expressive code that aligns with the signature of the functional interface's abstract method.
Comments
Post a Comment
Leave Comment