Java Tip: Creating Your Own Lambda Function Interface

Java Tip: Creating Your Own Lambda Function Interface

If you've been using Java 8+ for a while, you've probably seen lambda expressions like these:

Runnable r = () -> System.out.println("Running!");        

But did you know you can define your own functional interface to use lambdas tailored to your application’s logic?

In this post, I’ll walk you through how to create and use custom lambda functions in Java—a great way to write cleaner, more flexible, and testable code.


🧠 What is a Functional Interface?

A functional interface is an interface with only one abstract method. Java uses it as the target type for lambda expressions.

Example:

@FunctionalInterface
interface MathOperation {
    int operate(int a, int b);
}        

Now we can implement this using a lambda:

MathOperation add = (a, b) -> a + b;
MathOperation multiply = (a, b) -> a * b;

System.out.println(add.operate(5, 3));      // 8
System.out.println(multiply.operate(5, 3)); // 15        

✨ Benefits of Using Lambdas

✅ More readable code ✅ Fewer anonymous classes ✅ Encourages declarative programming ✅ Easy to pass behavior as parameters


🎯 Real-World Example: Custom Filter

Let’s say you want to filter a list of names based on different conditions.

Step 1: Define the functional interface

@FunctionalInterface
interface NameFilter {
    boolean filter(String name);
}        

Step 2: Use it in a method

public static List<String> filterNames(List<String> names, NameFilter filter) {
    List<String> result = new ArrayList<>();
    for (String name : names) {
        if (filter.filter(name)) {
            result.add(name);
        }
    }
    return result;
}        

Step 3: Call it with lambdas

List<String> names = List.of("Alice", "Bob", "Amanda", "Steve");

List<String> aNames = filterNames(names, name -> name.startsWith("A"));

System.out.println(aNames); // [Alice, Amanda]        

🔚 Wrapping Up

Lambdas are more than just syntactic sugar—they're a powerful feature that promotes clean and flexible code.

✅ Create your own functional interfaces ✅ Pass behavior as parameters ✅ Write less and do more


If you're still wrapping your head around lambdas or just starting with functional programming in Java, this is a great starting point!

💬 Have you created your own lambda-style interfaces? Share your use cases or ask questions below 👇

Very interesting, thanks Gilberto

Like
Reply
Flávio Meira

FrontEnd Software Engineer | JavaScript, TypeScript, ReactJS & Next.js Specialist | Accessibility a11y | MBA in FrontEnd Development & Software Architecture | iFood

20h

Thanks for sharing, Gilberto

Like
Reply
Aislan Freitas

Senior Data Science | Data Engineer | MLOps | Python Developer | Machine Learning | Big data | Gen AI | LLM | RAG | SQL | GCP

1d

Great post! Technology has the power to transform our daily lives, and it's inspiring to see how innovations are shaping the future. Always excited to see what's coming next!

Like
Reply
Elison G.

Senior Site Reliability Engineer(SRE) | Certified Cloud Engineer | DevOps Engineer | 3x NSE Fortinet | 1x GCP | CKAD(In Progress)

3d

Thoughtful post, thanks Gilberto Melo

Willian de Castro

Senior FullStack Developer | C# | Angular | React.js | Azure | RabbitMQ | Node.js | AWS | Sql Server | Oracle | Postgresql | Sqlite | MongoDB | Senior FullStack Engineer

3d

Very helpful

To view or add a comment, sign in

More articles by Gilberto Melo

Insights from the community

Others also viewed

Explore topics