Lambda Expression Fundamentals
Lambda Expression Fundamentals
A Lambda Expression in Java provides a clear and concise way to represent a function (or
block of code) that can be passed around and executed. It is primarily used to define the
behavior of methods in a functional style, which makes it easier to work with collections,
streams, and other functional programming paradigms.
Parameters: The list of parameters that the lambda expression accepts. It can be
empty, single, or multiple parameters.
Arrow (->): The lambda operator that separates parameters from the body.
Expression: A single expression or block of code that gets executed.
For example:
java
Code:
java
Code:
Here, the lambda expression (a, b) -> a + b represents the addition operation, which
is passed to the BinaryOperator functional interface.
2. Block Lambda Expressions
In some cases, you may need a more complex block of code inside the lambda expression.
This is done by enclosing the code in curly braces {}.
Example:
Java Code:
@FunctionalInterface
interface TriFunction<T, U, V, R> {
R apply(T t, U u, V v);
}
In this example, the block of code in the lambda expression multiplies the numbers and also
prints the result.
Lambda expressions can be passed as arguments to methods, which is very useful in the
context of functional interfaces (e.g., Runnable, Comparator, etc.).
Example:
Java Code:
import java.util.Arrays;
import java.util.List;
In this example:
Lambda expressions can throw exceptions, but it requires special handling because lambda
expressions are treated like anonymous methods, and throwing checked exceptions inside
them isn't allowed by default.
To throw exceptions from lambda expressions, we need to wrap them in a try-catch block or
declare them in the functional interface signature.
java
Code:
import java.util.Arrays;
import java.util.List;
In this example, we throw an exception inside the lambda expression if the word's length is
greater than 5, and it is caught and handled.
5. Variable Capture
Lambda expressions in Java capture variables from the enclosing scope. This means that
lambda expressions can access variables defined in their enclosing method, but they can only
access final or effectively final variables.
Example:
Java Code:
In this example, the multiplier variable is accessed inside the lambda expression.
Important: The variable multiplier must be either final or effectively final, meaning
its value cannot be changed after initialization.
6. Method References
Method references provide a shorthand for calling a method using a lambda expression. It
refers directly to a method using the :: operator.
java
Copy
public class MethodReferenceExample {
public static void main(String[] args) {
// Lambda expression to print each word
List<String> words = Arrays.asList("apple", "banana",
"cherry");
words.forEach(MethodReferenceExample::printWord);
}
// Static method
public static void printWord(String word) {
System.out.println(word);
}
}
java
Copy
public class MethodReferenceExample {
public static void main(String[] args) {
MethodReferenceExample example = new
MethodReferenceExample();
List<String> words = Arrays.asList("apple", "banana",
"cherry");
// Instance method
public void printWord(String word) {
System.out.println(word);
}
}
java
Copy
import java.util.*;
In this example, the Product::new reference can be used if we are creating new
Product objects via a constructor.
Summary
By using these features of Java, you can write more concise, functional-style code and take
advantage of the powerful capabilities of the Stream API, functional interfaces, and other
Java libraries.