Fundamentals 11 min read

Why Java Needs Lambda: Solving the Vertical Problem with Functional Interfaces

This article explains the need for Java lambda expressions by illustrating the "Vertical Problem" of repetitive, tightly‑coupled code for different contact methods and demonstrates a progression of refactorings that culminate in a concise solution using functional interfaces and predicates.

IT Xianyu
IT Xianyu
IT Xianyu
Why Java Needs Lambda: Solving the Vertical Problem with Functional Interfaces

In this article the author explains why Java needs lambda expressions by describing the "Vertical Problem"—the difficulty of writing repetitive, tightly‑coupled code for different contact methods—and shows step‑by‑step refactorings from a naïve implementation to a clean solution using functional interfaces.

The initial example defines a Person class and three separate methods ( callDrivers , emailDraftees , mailPilots ) that each contain similar loops and hard‑coded criteria, violating the DRY principle and making future extensions cumbersome.

First refactoring extracts the criteria into helper methods ( isDriver , isDraftee , isPilot ), but the core methods still depend on these checks, keeping the module tightly coupled to the criteria logic.

Next the author introduces a generic MyTest<T> interface to pass the criteria from outside, allowing the core module to remain unchanged while callers supply custom implementations via anonymous classes.

Finally, the article demonstrates how Java 8’s java.util.function.Predicate interface and lambda expressions replace the verbose anonymous classes, reducing the criteria to a single line such as p -> p.getAge() >= 16 . The RoboContactLambda class now accepts a Predicate<Person> for each contact method, making the code concise, reusable, and easily composable.

The author concludes that lambda expressions effectively solve the Vertical Problem by separating the core processing from the selection logic, improving readability and maintainability.

JavaLambdaFunctional ProgrammingrefactoringPredicatevertical-problem
IT Xianyu
Written by

IT Xianyu

We share common IT technologies (Java, Web, SQL, etc.) and practical applications of emerging software development techniques. New articles are posted daily. Follow IT Xianyu to stay ahead in tech. The IT Xianyu series is being regularly updated.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.