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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.