Finite generated Stream in Java - how to create one?
In Java, one can easily generate an infinite stream with Stream.generate(supplier) . However, I would need to generate a stream that will eventually finish. Imagine, for example, I want a stream of all files in a directory. The number of files can be huge, therefore I can not gather all the data upfront and create a stream from them (via collection.stream() ). I need to generate the sequence piece by piece. But the stream will obviously finish at some point, and terminal operators like ( collect() or findAny() ) need to work on it, so Stream.generate(supplier) is not suitable here. Is there