Why Streams Are Lazy

Streams are lazy because intermediate operations are not evaluated until terminal operation is invoked. … The pipeline accumulates these newly created streams. The time when terminal operation is called, traversal of streams begins and the associated function is performed one by one.

Can a stream be reused?

A stream should be operated on (invoking an intermediate or terminal stream operation) only once. A stream implementation may throw IllegalStateException if it detects that the stream is being reused. So the answer is no, streams are not meant to be reused.

What is the key benefit of lazy evaluation when you are dealing with a stream?

The benefits of lazy evaluation include: The ability to define control flow (structures) as abstractions instead of primitives. The ability to define potentially infinite data structures. This allows for more straightforward implementation of some algorithms.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest