Scenario: I have a collection of objects that I want to do some work to, transform them, and collect the results. Initially, I had two map calls:
objects.stream()
.map(o -> {
// do something interesting...
// set some fields...
return o;
})
.map(transformer::mapToSomethingElse)
.collect(Collectors.toList());
My IDE recommended using .peek
for the first map, since I'm just returning the same input. When I read the API notes for peek, I see the following:
This method exists mainly to support debugging
Is my usage of peek a bad idea? The operations happening within peek are necessary, so if they were optimized away, that would be bad. Due to the way this stream operation is structured, I don't have any reason to believe the peek would be elided.
Of course I could just write this with a for loop, but I like the syntax of streams so I use them wherever I can.