Yeah, in Java calling first()
on a stream is the same as an early return in a for-loop, where for each element all of the previous stream operations are applied first.
So the stream operation
cars.stream()
.filter(c -> c.year() < 1977)
.first()
is equivalent to doing the following imperatively
for (var car : cars) {
if (car.year() < 1977) return car;
}
Not to mention Kotlin actually supports non-local returns in lambdas under specific circumstances, which allows for even more circumstances to be expressed with functional chaining.
Well yes, I was simplifying because I wanted to address the main (incorrect) criticism by @spartanatreyu@programming.dev. I agree with your comment