I'm trying to form a cocise and conherent understanding of the application of lazy evaluation within the Java streams API.
Here is what I currently understand:
- elements are only consumed as they are needed, i.e. streams are lazy, and intermediate operations are lazy such that e.g. filter, will only filter when it is required to.
- intermediate operations may be fused together (if they are stateless).
- short-circuiting operations do not need to process the entire stream.
What I want to do is bring all these ideas together and ensure I'm not misrepresenting anything. I'm finding it tricky because whenever I read any literature on Java streams, it goes on to say they're lazy or utilise lazy evaluation, and then very much interchangeably starts talking about optimisations such as fusion and short-circuiting.
So would I be right in saying the following?
fusion is how lazy evaluation has been implemented in the stream API - i.e. an element is consumed, and operations are fused together wherever possible. I'm thinking that if fusion didn't exist then surely we'd be back to eager evaluation as the alternative would just be to process all elements for each intermediate operation before moving onto the next?
short-circuiting is possible without fusion or lazy evaluation but is very much helped in the context of streams by these the implementation of these two principles?
I'd appreciate any further insight and clarity on this.