import java.util.stream.*;
public class MyClass {
public static void main(String args[]) {
Stream.of(1, 2, 3).map(i -> {
System.out.println(i+":inside map");
return i+4;
}).forEach(t->System.out.println(t + ":inside foreach"));
}
}
// Prints:
1:inside map
5:inside foreach
2:inside map
6:inside foreach
3:inside map
7:inside foreach
Shouldn't the output be :
1:inside map
2:inside map
3:inside map
5:inside foreach
6:inside foreach
7:inside foreach
I was under the impression that after each intermediate operation ends, it returns a new stream. So foreach should have gotten (5,6,7) and therefore print
5:inside foreach
6:inside foreach
7:inside foreach
But the case shows that foreach is executed for each entry of map one by one. Why is that?