Lambda Expressions
Lambda Expressions are one of the most important features introduced in Java 8. They allow you to write shorter and more readable code by replacing anonymous classes with concise syntax.
What Are Lambda Expressions?β
A Lambda Expression is a block of code that can be passed around and executed. It is used to implement functional interfaces, which are interfaces with only one abstract method.
Syntax of Lambda Expressionsβ
The basic syntax of a Lambda Expression is:
(parameters) -> expression
- Parameters: The input values for the Lambda.
- Arrow (
->): Separates the parameters from the body. - Expression: The code to execute.
Example: Using Lambda Expressionsβ
Hereβs an example of how Lambda Expressions simplify code:
// Without Lambda Expression
Runnable runnable = new Runnable() {
public void run() {
System.out.println("Hello, World!");
}
};
// With Lambda Expression
Runnable runnableLambda = () -> System.out.println("Hello, World!");
// Execute the Runnable
new Thread(runnableLambda).start();
Output:
Hello, World!
Explanation:
- The Lambda Expression
() -> System.out.println("Hello, World!")replaces the anonymous class implementation. - It makes the code shorter and easier to read.
Functional Interfacesβ
Functional Interfaces are interfaces with exactly one abstract method. Java 8 provides several built-in functional interfaces in the java.util.function package.
Common Functional Interfacesβ
-
**Predicate<T>**: Takes an argument and returns a boolean.Predicate<Integer> isEven = num -> num % 2 == 0;
System.out.println(isEven.test(4)); // Output: true -
**Function<T, R>**: Takes an argument of typeTand returns a result of typeR.Function<String, Integer> stringLength = str -> str.length();
System.out.println(stringLength.apply("Java")); // Output: 4 -
**Consumer<T>**: Takes an argument and performs an action without returning a result.Consumer<String> printMessage = message -> System.out.println(message);
printMessage.accept("Hello, Lambda!"); // Output: Hello, Lambda! -
**Supplier<T>**: Supplies a value of typeT.Supplier<Double> randomValue = () -> Math.random();
System.out.println(randomValue.get()); // Output: A random double value
Key Takeawaysβ
- Use Lambda Expressions to replace anonymous classes for functional interfaces.
- Functional Interfaces like
Predicate,Function,Consumer, andSupplierare part of thejava.util.functionpackage. - Lambda Expressions make your code shorter, cleaner, and easier to understand.