O'Reilly logo

Learning Java Functional Programming by Richard M Reese

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required