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 print statements. Using a debugger will eliminate the need for this extra work.
Using the peek method to assist debugging
Before we illustrate...