Source: consolidated from the streams/lambdas/Optional material that used to live in fundamentals, drawn from both projects’ real usage (the SubscriptionService stream filters and OptionalInt config lookups are actual code), plus the roadmap.sh Java functional nodes (higher-order functions, composition) the projects didn’t name explicitly.

Functional interfaces and lambdas

A functional interface has exactly one abstract method, so a lambda can stand in for an instance of it. This is the bridge between Java’s OO core and functional style:

@FunctionalInterface
public interface DeliverCallback { void handle(String tag, Delivery msg) throws IOException; }
 
DeliverCallback cb = (tag, delivery) -> process(delivery);   // lambda replaces an anonymous class

Anonymous classes — what lambdas replaced

Before lambdas, the way to supply a one-off implementation inline was an anonymous class: new SomeType() { ... } creates an unnamed class and an instance of it in one expression, right where it’s needed — no separate named class, used once. It’s still the tool when the interface has more than one method (so a lambda can’t apply), as with TimerTask (Core APIs):

Runnable r = new Runnable() {              // anonymous class implementing Runnable
    @Override public void run() { System.out.println("running"); }
};
 
// For a single-abstract-method interface, the lambda is the same thing, shorter:
Runnable r2 = () -> System.out.println("running");

An anonymous class can capture effectively-final locals and can hold its own fields/multiple methods — the two things a lambda can’t. It’s a local/inner class distinction covered structurally in OOP; here the point is the lambda-vs-anonymous-class tradeoff.

The java.util.function package supplies the general-purpose ones you’ll use constantly:

InterfaceShapeTypical use
Function<T,R>T -> Rtransform/map
Predicate<T>T -> booleanfilter/test
Consumer<T>T -> voidside effect (forEach)
Supplier<T>() -> Tlazy/deferred value
BiFunction<T,U,R>(T,U) -> Rtwo-arg transform

A lambda captures surrounding local variables, which must be effectively final (assigned once) — the same rule from Fundamentals, and why loop bodies sometimes copy a counter into a final local before using it in a lambda.

Method references

When a lambda just calls one existing method, a method reference says so more directly — four forms:

list.forEach(System.out::println);              // instance method of an arbitrary object
names.stream().map(String::toUpperCase)          // instance method on the stream element
     .map(Transaction::new)                      // constructor reference
     .sorted(Comparator.comparing(Transaction::getAmount));   // getter reference

Transaction::getAmount reads as “the function that, given a Transaction, returns its amount” — the same thing as t -> t.getAmount(), just without the noise.

The Stream API

A stream is a pipeline over a data source: a lazy chain of intermediate operations (filter, map, sorted, distinct, limit) ending in one terminal operation (collect, toList, count, reduce, findFirst, anyMatch) that actually runs the pipeline:

List<SubscriptionRecord> active = subscriptions.values().stream()
        .filter(s -> s.getDebitAccount().equals(account))   // intermediate — lazy
        .filter(s -> "ACTIVE".equals(s.getStatus()))
        .sorted(Comparator.comparing(SubscriptionRecord::getCreatedAt))
        .toList();                                            // terminal — triggers execution

Laziness matters: nothing runs until the terminal op, and short-circuiting terminals (findFirst, anyMatch) stop early instead of processing the whole source. Collectors build richer results — groupingBy, toMap, joining, counting:

Map<String, List<Transaction>> byCurrency =
        txns.stream().collect(Collectors.groupingBy(Transaction::getCurrency));

When not to reach for a stream: a simple index-based loop, or one that mutates external state, is often clearer as a plain for. Streams shine for declarative transform/filter/aggregate pipelines, not for everything. And a parallel stream (.parallelStream()) is rarely the right lever — it splits work across the common ForkJoinPool and only helps for large, CPU-bound, side-effect-free workloads; on small or IO-bound data it’s slower and riskier than a plain loop.

Higher-order functions and composition

A higher-order function takes or returns a function — map, filter, and Comparator.comparing all are. Function and Predicate compose, which lets you build complex behavior from small named pieces:

Predicate<Transaction> isLarge  = t -> t.getAmount().compareTo(THRESHOLD) > 0;
Predicate<Transaction> isGhs    = t -> "GHS".equals(t.getCurrency());
Predicate<Transaction> flag     = isLarge.and(isGhs);            // combine predicates
 
Function<Transaction, String> describe =
        ((Function<Transaction,BigDecimal>) Transaction::getAmount)
                .andThen(BigDecimal::toPlainString);            // f.andThen(g) = g(f(x))

Comparator composition (comparing(...).thenComparing(...).reversed()) is the most common real use — see Collections.

Method chaining and fluent APIs

Returning this (or a new immutable copy) from each method lets calls chain into a readable pipeline — the basis of builders, streams, and Optional:

SubscriptionRecord.builder().subscriptionId("SUB1").debitAmount("50.00").build();

The design principle is that each step returns something the next step can act on. Builders are covered in Lombok & Builders.

Optional — absence as a type

Optional<T> makes “might be missing” explicit in the signature instead of relying on null and hoping callers check:

public Optional<CountryDialingCode> fromIso(String iso) {
    return Arrays.stream(values()).filter(c -> c.matches(iso)).findFirst();
}
 
// Consuming it — no null check, no NPE:
String prefix = fromIso(country)
        .map(CountryDialingCode::prefix)
        .orElse("unknown");

Key operations: map / flatMap (transform if present), filter, orElse / orElseGet / orElseThrow, ifPresent. OptionalInt/OptionalLong/OptionalDouble are the primitive-specialized versions that avoid boxing — the payment sandbox uses OptionalInt for reading a numeric config value that might be absent:

OptionalInt retryAttempts = getConfigIntValue(config, "retryAttempts");
if (retryAttempts.isPresent() && retryAttempts.getAsInt() > 1) { /* validation error */ }

Style rule: use Optional as a return type to signal optionality; don’t use it for fields or method parameters (it adds allocation and awkwardness there).

  • Generics — the <T,R> type parameters on every functional interface
  • Collections — what streams flow over
  • Modern Java — pattern matching pairs with functional style
↑ Back to 01 — Language