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
1. No Parameters, No Return Value:
() -> System.out.println("Hello, World!");
2. Single Parameter, No Type Declaration:
s -> System.out.println(s);
3. Single Parameter, With Type Declaration:
(String s) -> System.out.println(s);
4. Multiple Parameters:
(a, b) -> a + b;
(int a, int b) -> a + b;
5. Return Statement:
(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:
() -> 42;
7. Use Local Variables:
int factor = 2;
(int a) -> a * factor;
Example: Various Forms of Lambda Expressions
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
Comments
Post a Comment
Leave Comment