Returns a stream consisting of the elements of this stream, truncated
to be no longer than maxSize in length.
This is a short-circuiting stateful intermediate operation.
maxSize | the number of elements the stream should be limited to |
IllegalArgumentException | if maxSize is negative |
@apiNote
While limit() is generally a cheap operation on sequential
stream pipelines, it can be quite expensive on ordered parallel pipelines,
especially for large values of maxSize, since limit(n)
is constrained to return not just any n elements, but the
first n elements in the encounter order. Using an unordered
stream source (such as generate(Supplier)) or removing the
ordering constraint with unordered() may result in significant
speedups of limit() in parallel pipelines, if the semantics of
your situation permit. If consistency with encounter order is required,
and you are experiencing poor performance or memory utilization with
limit() in parallel pipelines, switching to sequential execution
with sequential() may improve performance.
Diagram: Stream