2. Lambda Expressions
Lambda expressions were introduced in Java 8 and are a powerful feature that
allows you to pass behavior as parameters to methods. They provide a clear and
concise way to express instances of single-method interfaces (functional
interfaces) using an expression.
Lambda expressions are commonly used to define the implementation of
functional interfaces in a more compact way. They are particularly useful for
working with the Java Streams API, but they can be used in any context where a
functional interface is required.
3. Syntax of Lambda Expressions
(parameters) -> expression
(Or, if there's a block of code)
(parameters) -> { statements; }
Components of a Lambda Expression:
● Parameters: The input parameters (if any) to the lambda expression,
enclosed in parentheses. The types can often be inferred, so you may omit
them (type inference).
● Arrow Token (->): Separates the parameters from the body.
● Expression or Block of Code: The body of the lambda expression. If it's a
single expression, it can return a value directly. If it's a block of code, it may
contain multiple statements.
4. Simple Lambda Expression
// A lambda expression that adds two numbers
BinaryOperation add = (a, b) -> a + b;
System.out.println(add.operation(5, 3)); // Output: 8
5. Lambda Expression with Multiple Parameters:
// Lambda expression with two parameters
BiPredicate<Integer, Integer> isGreater = (x, y) -> x > y;
System.out.println(isGreater.test(10, 5)); // Output: true
6. Lambda Expression with No Parameters:
Runnable helloWorld = () -> System.out.println("Hello, World!");
helloWorld.run(); // Output: Hello, World!
7. Lambda Expression with Multiple Statements:
Function<Integer, Integer> squareAndAddOne = (x) -> {
int square = x * x;
return square + 1;
};
System.out.println(squareAndAddOne.apply(5)); // Output: 26
8. Benefits of Lambda Expressions
Concise Code: They allow you to express instances of single-method interfaces in
a more compact and readable form.
Cleaner Code: They make your code more functional and less cluttered by
reducing boilerplate code, especially in scenarios like event handling, threading,
etc.
Improved Readability: Code that was previously verbose can now be written more
clearly, especially when using the Streams API or when working with collections.
Increased Flexibility: You can pass behavior (code) as parameters, which allows
you to write more generic and reusable code.
9. Streams API
The Streams API provides a high-level abstraction for performing complex data manipulations (such as
filtering, mapping, and reducing) on collections in a functional style.
It allows you to perform operations on collections in a more declarative, functional, and parallelizable
manner.
Common Stream Operations:
● filter: Filters elements based on a condition.
● map: Transforms each element.
● reduce: Reduces elements to a single value.
● forEach: Iterates over elements.
● collect: Collects elements into collections (e.g., lists, sets).
10. Lambda Expressions with Streams
Lambda expressions are commonly used with the Streams API, which is part of Java's functional programming
capabilities.
The Streams API allows you to process sequences of elements in a functional style.
Example: Using lambda expression to filter and print even numbers
import java.util.List;
import java.util.Arrays;
public class LambdaWithStream {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(n -> System.out.println(n)); // Output: 2 4
}
}