Lesson · Lambdas & Functional Interfaces
Why Functional Java: passing behavior, not just data
13 min readWhy Functional Java
Java always let you hand a behavior to a method, it just cost you an anonymous class and a pile of punctuation. Java 8 made that pattern cheap, and half the modern standard library is built on the result.
The pattern that was always there, priced too high
Sorting a list by a rule you choose is an old job. Before Java 8 you could do it, but the rule itself was an object, so you had to write a whole class just to express one expression.
Every callback, comparator, listener and policy flag had this shape: the useful part was one line, and the ceremony around it was seven. Java was not missing the ability to pass behavior. It was missing a readable way to write it.
// 1997 style: a class whose only purpose is one return statement players.sort(new Comparator<Player>() { @Override public int compare(Player a, Player b) { return Integer.compare(b.score(), a.score()); } });
Behavior parameterization: the idea under everything
The shift Java 8 invited is to treat a rule as a value. Write one method that takes the rule as a parameter, and every variation of the job becomes one line at the call site instead of one class in a file.
This is the single idea behind lambdas, method references, streams, Optional and CompletableFuture. The rest of Part V is variations on it.
static List<Player> filter(List<Player> in, Predicate<Player> rule) { List<Player> out = new ArrayList<>(); for (Player p : in) { if (rule.test(p)) out.add(p); } return out; } List<Player> high = filter(players, p -> p.score() >= 90); List<Player> shortNames = filter(players, p -> p.name().length() <= 5);
What Java 8 actually added
Five features landed together and they only make sense as a set: lambdas give the syntax, functional interfaces give the target types, method references shorten common lambdas, streams use both to process collections, and default methods let existing interfaces grow without breaking implementors.
| Feature | What it is | Where you meet it |
|---|---|---|
| Lambda expression | A behavior written inline, no class needed | anywhere a functional interface is expected |
| Functional interface | An interface with exactly one abstract method | Predicate, Function, Consumer, Supplier, Runnable |
| Method reference | A lambda that only calls an existing method | String::length, System.out::println |
| Stream API | Pipeline processing for collections | filter, map, collect, groupingBy |
| Default methods | Interface methods with a body | List.sort, Collection.removeIf, Comparator.thenComparing |
Behavior board · one job, two eras
pick a job, flip the eraOne rule, seven lines of ceremony before, one expression after. The behavior was never the problem.
What it buys you
Not every loop should become a stream, and no interviewer will accept functional style as a substitute for understanding. But three things get genuinely better:
- Noise drops. The one meaningful line stops hiding inside nine lines of boilerplate.
- Rules compose. pred.and(other), comparator.thenComparing(next), function.andThen(step) turn small behaviors into bigger ones without new classes.
- Reading changes direction. A pipeline states what is wanted (keep scores over 90, take names, sort) instead of how to walk an index.
Functional style in Java is a reading and composition win first, a performance topic second. Write the version you can defend in review.
Where you will meet it, willing or not
You already call functional APIs daily even if you never write a lambda yourself: List.sort, Collection.removeIf, Map.computeIfAbsent, Optional.map, CompletableFuture.thenApply and every stream all take a behavior as a parameter. Reading them fluently is now a baseline Java skill, and the interview questions follow the same trail.
Nothing became impossible in 1997
Anonymous classes could always pass behavior; lambdas removed the tax, not a limitation. That is why a lambda can be assigned to any interface with one abstract method, however old that interface is (Runnable since 1.0, Comparator since 1.2).
Interview angles
What problem did lambdas actually solve?+
Verbosity of passing behavior. Anonymous classes could express a callback but buried one line of logic in ceremony. Lambdas make the common case (a rule, a mapping, a callback) one expression, which made API design like the Streams library practical.
Is Java a functional programming language now?+
No. It gained first-class functions in syntax but keeps mutable state, classes, and imperative style as equals. Lambdas, streams and Optional are functional-flavored tools inside a mostly imperative language.
What is behavior parameterization?+
Turning a rule into a value so a method can receive it. The method stays generic (filter, sort, map) and the caller supplies the specific rule as a lambda, a method reference, or an old-school object that implements the interface.
Why do lambdas require effectively final captured variables?+
The lambda captures the variable's value at creation, not a live reference to the variable. Allowing reassignment afterwards would create a second, invisible copy whose updates did not flow, so the compiler forbids the confusing case.
Name three places the JDK hands you behavior-parameterized methods.+
List.sort(Comparator), Collection.removeIf(Predicate), Map.computeIfAbsent(key, Function), Optional.map/ifPresent, Comparator.thenComparing. Streams are the largest surface but far from the only one.