I know that stream intermediate operations in Java are lazy and do not get executed until a terminal operation is being called. However, I'm not sure whether chaining intermediate operations increases the number of iterations required. Because sometimes we separate operations into multiple intermediate operations, just to make it easier to read.
So for instance the following two examples do the same thing. But in the second example, two intermediate operations are joined into one.
myStream
.map(s -> methodA(s))
.map(s -> methodB(s))
.collect(Collectors.joining());
myStream
.map(s -> methodB(methodA(s)))
.collect(Collectors.joining());
Aren't the number of iterations required is same in both cases? Because it seems, it is possible for the JVM to execute it in a single iteration (similar to a single for-loop), rather than iterating over the elements for each intermediate operation.