IntStream.range(x,y)
would return a stream from x(inclusive) and y(exclusive).
IntStream.rangeClosed(x,y)
would return a stream from x(inclusive) and y(inclusive).
I expected rangeClosed(x,y)
to invoke range(x,y-1)
or range(x,y)
to invoke rangeClosed(x,y-1)
. But while looking at the source code for range
it was like:
if (startInclusive >= endExclusive) {
return empty();
} else {
return StreamSupport.intStream(
new Streams.RangeIntSpliterator(startInclusive, endExclusive, false /*not closed*/), false);
}
The rangeClosed
also had a very similar implementation rather than range(x,y+1)
. The only difference was that the third argument to Streams.RangeIntSpliterator
was true
instead of false
denoting that the range is closed.
This boolean is then used to initialize int last
field in Streams.RangeIntSpliterator
class and the below comment is mentioned against it:
1 if the range is closed and the last element has not been traversed Otherwise, 0 if the range is open, or is a closed range and all elements have been traversed
Why is such an implementation necessary instead of range
simply calling rangeClosed
or the other way round? Is there any significant difference between calling rangeClosed(x,y)
instead of range(x,y+1)
?