Get a Slice of a Stream
suggest changeExample: Get a Stream of 30 elements, the 21st through 50th elements of a collection (1-based positions).
final long n = 20L; // the number of elements to skip
final long maxSize = 30L; // the number of elements the stream should be limited to
final Stream<T> slice = collection.stream().skip(n).limit(maxSize);
Notes:
IllegalArgumentExceptionis thrown ifnis negative ormaxSizeis negative- both
skip(long)andlimit(long)are intermediate operations - if a stream contains fewer than
nelements thenskip(n)returns an empty stream - both
skip(long)andlimit(long)are cheap operations on sequential stream pipelines, but can be quite expensive on ordered parallel pipelines
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents