Debugging Lambdas and Streams [EN]

Streams provide a high level abstraction. This is good for making code clear and easy to understand, but it is bad for debugging. But the peek() method can be used to find what is happening between methods.

A simple example :

List<String> list = Arrays.asList(
"BarCamp", "MongoDB", "10Gen", "TokuMX", "Nagios", "PUCMM", "Ruby", "JSON",
"JSON");

List<String> newList1 = list
.stream()
.filter(s -> (s.length() % 2)==1)
.map(String::toLowerCase)
.peek(System.out::println)
.collect(Collectors.toList());

Also, you can use the peek method to put a breakpoint while you are debugging with your favorite IDE.

List<String> newList1 = list
.stream()
.filter(s -> (s.length() % 2)==1)
.map(String::toLowerCase)
.peek(s -> s.toString())
.collect(Collectors.toList());

The breakpoint must be set in the peek method.