I know there is infinite stream in Java.
Is there a way to check whether the stream is finite or not?
Something like this method isStreamFinite(Stream<T> stream)
?
@Test
public void testStreamFinity() {
assertFalse(isStreamFinite(generateLong()));
}
private <T> boolean isStreamFinite(Stream<T> stream) {
if (stream.count() < Long.MAX_VALUE) {
return true;
}
return false;
}
private Stream<Long> generateLong() {
return LongStream.generate(() -> new Random().nextLong()).boxed();
}
The construction of the Stream would leave some marks/tracks that we can use to trace back to check it?
The checking method could work always as expected reliably?
Update
I was wrong about this when trying to solve a problem by detecting the isFinite
of a stream. It seems it's not stable/reliable as the answers mentioned. I will refactor my solution in another around. Thank you for the help ~