Java Lambda Expressions Explained in Under 2 Minutes
Understanding Lambda Expressions
A lambda expression is essentially an anonymous function that provides an implementation for the single abstract method found within a functional interface.
But here is the real magic: this tiny, one-line syntax replaces the need for an entire traditional class implementation, simply by providing the logic for that single abstract method.
Traditional vs. Lambda Approach
Let's explore the difference between the conventional approach and the modern Lambda approach.
The Traditional Approach
With the traditional method, you would typically use an external class and an overriding method to provide the implementation, which can be verbose.
// Example of a traditional implementation
class EmailValidatorImpl implements EmailValidator {
@Override
public boolean isValid(String email) {
return email != null && email.contains("@") && email.endsWith(".com");
}
}
The Lambda Approach
When you switch to the Lambda approach, the code becomes incredibly streamlined. There's no external class, no method overriding, and no clutter. It's simple and clean. In fact, the entire Lambda expression is assigned to a reference of a functional interface type.
// The same logic with a Lambda expression
EmailValidator validator = (email) -> email != null && email.contains("@") && email.endsWith(".com");
Real-World Scenario: Email Validation in Just a Few Steps
Let's consider a practical, real-world scenario: email validation. Imagine we are building a backend service where users sign up with their email addresses, and we need to validate these incoming emails.
The validation rules are simple: 1. The email must not be null. 2. It must contain the "@" symbol. 3. It must end with ".com".
Step 1: Define the Functional Interface
First, we create a functional interface called EmailValidator
with a single abstract method, isValid
.
@FunctionalInterface
interface EmailValidator {
boolean isValid(String email);
}
Step 2: Write the Lambda Expression
Now, let's write the Lambda expression for this interface. The basic syntax involves parameters, the lambda operator (->
), and a body.
We'll assign this expression to a reference of our EmailValidator
interface.
EmailValidator validator = (email) -> {
// Validation logic goes here
};
Let's complete the expression. The parameter email
corresponds to the parameter in our abstract isValid
method. We don't need to specify its type (String
), as the Java compiler automatically infers it.
Inside the lambda body, we provide the validation logic. To keep it concise, we can write it in a single line, which allows us to remove the curly braces.
EmailValidator validator = (email) -> email != null && email.contains("@") && email.endsWith(".com");
And that's it! Our Lambda expression is complete.
Step 3: Test the Lambda Expression
Now, let's see how our Lambda expression works with a couple of examples. We'll call the isValid
method using our functional interface reference.
public class Main {
public static void main(String[] args) {
EmailValidator validator = (email) -> email != null && email.contains("@") && email.endsWith(".com");
// Test with a valid email
System.out.println(validator.isValid("[email protected]"));
// Test with an invalid email
System.out.println(validator.isValid("invalid-email"));
}
}
When we run this class, we get the following output:
true
false
The first input is correctly identified as valid because it is not null, contains the "@" symbol, and ends with ".com". The second input fails the validation, proving our Lambda expression works as intended.
Join the 10xdev Community
Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.