Arisk
Arisk

Reputation: 13

Restrictions when working with unbounded streams

Are their any restrictions when working with unbounded(infinite) streams in java? if so, what are the restrictions?

Upvotes: 0

Views: 324

Answers (1)

Andreas
Andreas

Reputation: 159165

Don't call a terminal operation that isn't short-circuiting, unless you've applied a short-circuiting intermediate operation.

Short-circuiting terminal operations:

Short-circuiting intermediate operations:

E.g. the following will never return:

int max = IntStream.iterate(0, i -> (i + 1) % 100)
        .max().getAsInt();

But this will:

int max = IntStream.iterate(0, i -> (i + 1) % 100)
        .limit(15)
        .max().getAsInt();

For the operations that take a Predicate, there is still a risk of the terminal operation never returning.

Upvotes: 1

Related Questions