Using the println method to assist debugging

While not necessarily the best debugging approach, using print statements will be sufficient for some problems. The next code sequence rewrites the previous lambda expression to use println methods before and after the concatenation operation:

    list.stream()
            .map(s -> {
                System.out.println("Before: " + s);
                s += "-" + s.toLowerCase();
                System.out.println("After: " + s);
                return s;
            })
            .forEach(s -> System.out.println(s));

The output of this code sequence behaves as you would expect:

Before: Huey
After: Huey-huey
Huey-huey
Before: Dewey
After: Dewey-dewey
Dewey-dewey
Before: Louie
After: Louie-louie
Louie-louie

However, this is awkward and requires adding a body to the expression, a return statement, and the ...

Get Learning Java Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.