0

I have a program to read data from multiple sources, use a tournament tree to merge sort them, pack the data into blocks and output the blocks. I have implemented this as a function, which returns null when no more block is available.

DataBlock buildBlock()

Now I want to output a stream of blocks, but the only method I have found so far is Stream.generate which generates an infinite stream. My stream is of course not infinite. What is a proper way to generate a finite stream from this function?

Harper
  • 1,575
  • 13
  • 30
  • You can `limit` it. But that depends on the source of your stream. – Naman Oct 19 '19 at 16:18
  • You are right. I do not know the actual amount of data until I can read no more from the source, so I have no idea in advance how many blocks to limit – Harper Oct 19 '19 at 16:19
  • You seem to be looking for a [while loop equivalent of streams?](https://stackoverflow.com/questions/44700006/is-there-java-stream-equivalent-to-while-with-variable-assignment) – Naman Oct 19 '19 at 16:42

1 Answers1

0

If you use at least Java 9, you can apply takeWhile(Objects::nonNull) on your stream. If you use the older Java version, check out this question.

IlyaMuravjov
  • 2,204
  • 1
  • 8
  • 23