Stream.skip()
Skips the first N elements of a stream.
Available since: Java 8
Syntax
Stream<T> skip(long n)Parameters
| Parameter | Type | Description |
|---|---|---|
| n | long | How many leading elements to skip. |
Return value
Stream<T>The stream without its first `n` elements.
Examples
The second page of three elements:
void main() {
List<Integer> items = List.of(1, 2, 3, 4, 5, 6, 7, 8);
int page = 2, size = 3;
IO.println(items.stream().skip((long) (page - 1) * size).limit(size).toList());
}Output:
[4, 5, 6]Notes
Skipping more elements than there are gives an empty stream, without an exception. To skip while a condition holds, use dropWhile().
See also
- Stream.limit() — Truncates a stream to its first N elements.
- Stream.dropWhile() — Drops elements while a condition holds and passes on everything after.
Related recipes
In the tutorial
- Stream operations — The main Stream API operations — filter, map, sorted, distinct, limit, skip, count, reduce, anyMatch, findFirst and numeric streams.