Can stream restriction # return fewer items than expected?

If sthere are at least nelements in the stream below , what are the situations where the stream sLimitmay have fewer elements n, if any?

Stream sLimit = s.limit(n);

Cause of question: in this answer , I read that:

Despite the visibility, use limit(10)does not necessarily result in a stream SIZEDwith exactly 10 elements - it may be less.

+4
source share
2 answers

I think the answers of Holger and Sotirios are accurate, but since I'm the guy who made this expression, I think I have to explain it myself.

, SIZED. "" , , . , , , .

limit() , , limit , . SIZED, , SIZED.

, , , :

IntStream.range(0, 100).limit(10)

, , 10 . ( .) spliterator SIZED. , limit :

IntStream.range(0, 1).limit(10)

.

, , limit . , , , .

+5

. Stream n , limit(n) , n, Stream , , .

, Stream (Spliterator s) , , . Stream IntStream IntStream.range. , Stream limit(n).

parallel Stream Stream.generate(MyClass::new).limit(10), - , . , IntStream.range(0, n).mapToObj(i -> new MyClass()) Stream, , .

+6

All Articles