Reputation: 13
Are their any restrictions when working with unbounded(infinite) streams in java? if so, what are the restrictions?
Upvotes: 0
Views: 324
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:
findAny()
findFirst()
allMatch(Predicate<? super T> predicate)
anyMatch(Predicate<? super T> predicate)
noneMatch(Predicate<? super T> predicate)
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