Master Java 8 Lambdas and Streams: From Basics to Recursive Functions

This article explains Java 8 lambda expressions, their syntax, how to use streams with map, filter, and reduce operations, defines functional interfaces, and demonstrates advanced recursive lambda techniques such as the Y combinator, all with clear code examples.

BiCaiJia Technology Team
BiCaiJia Technology Team
BiCaiJia Technology Team
Master Java 8 Lambdas and Streams: From Basics to Recursive Functions

Lambda Expressions

A lambda expression is an anonymous function derived from the mathematical λ‑calculus, introduced in Java 8 to make code more concise.

General syntax:

(Type param) -> {
    statement1;
    statement2;
    return statementN;
}

When the parameter type can be inferred, the syntax simplifies to:

(param) -> {
    statement1;
    statement2;
    return statementN;
}

If there is only one parameter, parentheses may be omitted:

param -> {
    statement1;
    statement2;
    return statementN;
}

For a single statement, braces, the return keyword, and the trailing semicolon can be omitted:

param -> statement

Stream API

Example code:

List<Integer> list = Arrays.asList(1, 2, 3, 4);
list.stream()
    .map(i -> i * i)
    .filter(i -> i < 10)
    .reduce((a, b) -> a + b);

This creates a stream, maps each element to its square, filters values less than 10, and reduces the remaining elements by summing them.

map : transforms each element, producing a new stream of 1, 4, 9, 16.

filter : keeps elements that satisfy the predicate, resulting in 1, 4, 9.

reduce : repeatedly applies a binary function to combine elements, yielding the sum.

The collect() method can gather stream results into a List or Map:

List<Integer> collect = list.stream()
    .map(i -> i * i)
    .filter(i -> i < 10)
    .collect(Collectors.toList());
Map<Integer,Integer> collect = list.stream()
    .collect(Collectors.toMap(i -> i, i -> i * i));

Functional Interfaces

Java 8 defines a functional interface as an interface with a single abstract method, marked with @FunctionalInterface. Example:

@FunctionalInterface
public interface Function<T,R> {
    R apply(T t);
}

Such an interface can be instantiated with a lambda: Function<Integer,Integer> f = x -> x + 1; Calling f.apply(5) returns 6.

Recursive Lambdas

Recursion can be expressed with lambdas by defining a self‑applicable functional interface and using the Y combinator. Example interface:

@FunctionalInterface
public interface SelfApplicable<T,R> {
    Function<T,R> apply(SelfApplicable<T,R> self);
}

Define a step function g:

Function<Function<Integer,Long>,Function<Integer,Long>> g =
    function -> {
        return i -> i == 0 ? 0 : i + function.apply(i-1);
    };

Construct the Y combinator:

Function<Function<Function<Integer,Long>,Function<Integer,Long>>,Function<Integer,Long>> Y = f -> {
    SelfApplicable<Integer,Long> h = x -> (Integer n) -> f.apply(x.apply(x)).apply(n);
    return h.apply(h);
};

Compute the sum of numbers 1 to 10: Long sum = Y.apply(g).apply(10); // result: 55 While not common in everyday development, this demonstrates that Java lambda expressions can be used recursively.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

recursionFunctional InterfaceStreamsJava8
BiCaiJia Technology Team
Written by

BiCaiJia Technology Team

BiCaiJia Technology Team

0 followers
Reader feedback

How this landed with the community

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.