Java Stream skip
last modified May 25, 2025
This article demonstrates how to use the Java Stream skip method to bypass elements at the beginning of a stream.
The skip method is an intermediate operation in Java Streams that allows selective omission of the first N elements from a stream. It returns a new stream containing only the remaining elements, preserving their original order.
If the stream has fewer than N elements, an empty stream is returned, ensuring safe and predictable behavior in cases where the requested number of skipped elements exceeds the stream's size.
Basic skip syntax
The skip
method allows you to bypass a specified number of elements
at the start of a stream, returning a new stream with the remaining elements.
The method signature for skip is:
Stream<T> skip(long n)
The parameter n is the number of leading elements to skip. For ordered streams, the first n elements are discarded in encounter order. For unordered streams, any n elements may be discarded.
Skipping elements in ordered stream
You can use skip
with ordered streams to ignore the first n
elements and process the rest in their original order.
Basic usage with an ordered stream.
void main() { Stream.of("A", "B", "C", "D", "E", "F") .skip(3) .forEach(System.out::println); }
This example skips the first 3 elements ("A", "B", "C") and processes the remaining elements ("D", "E", "F").
$ java Main.java D E F
Skipping more elements than available
If you skip more elements than the stream contains, the result is an empty stream.
Behavior when skipping more elements than the stream contains.
void main() { long count = Stream.of(1, 2, 3) .skip(5) .count(); System.out.println("Remaining elements: " + count); }
This example attempts to skip 5 elements from a stream of only 3 elements, resulting in an empty stream with count 0.
$ java Main.java Remaining elements: 0
Combining skip with limit
Combining skip
and limit
enables efficient pagination
by selecting a specific range of elements from a stream.
Implementing pagination with skip
and limit
.
void main() { int pageSize = 5; int pageNumber = 2; // zero-based IntStream.rangeClosed(1, 20) .skip(pageNumber * pageSize) .limit(pageSize) .forEach(System.out::println); }
This example demonstrates pagination by skipping elements equivalent to previous pages and limiting to the current page size.
$ java Main.java 6 7 8 9 10
Skipping in unordered streams
When used with unordered streams, skip
may discard any n elements,
and the remaining elements have no guaranteed order.
Behavior with unordered streams.
void main() { Stream.of("A", "B", "C", "D", "E", "F") .unordered() .skip(3) .forEach(System.out::println); }
With unordered streams, skip may discard any elements rather than strictly the first ones. The remaining elements maintain no particular order.
Skipping after filtering
The order of skip
and filter
affects which elements
are skipped, as skip operates on the filtered stream.
Interaction between skip and filter operations.
void main() { IntStream.rangeClosed(1, 10) .filter(n -> n % 2 == 0) // even numbers .skip(2) // skip first 2 even numbers .forEach(System.out::println); }
This example first filters even numbers (2, 4, 6, 8, 10) then skips the first 2 of those (2, 4), resulting in 6, 8, 10.
$ java Main.java 6 8 10
CSV processing
A common use case for skip
is to ignore header rows when processing
CSV data.
Skipping header rows in CSV data.
void main() { String csvData = """ ID,Name,Age 1,John,25 2,Jane,30 3,Bob,28 """; Stream.of(csvData.split("\n")) .skip(1) // skip header row .map(line -> line.split(",")) .forEach(fields -> System.out.println( "Name: " + fields[1] + ", Age: " + fields[2])); }
This common use case demonstrates skipping the header row when processing CSV data.
$ java Main.java Name: John, Age: 25 Name: Jane, Age: 30 Name: Bob, Age: 28
Source
Java Stream skip documentation
In this article we have explored the Java Stream skip
method. It
provides a convenient way to bypass leading elements in a stream, with
particular usefulness in pagination scenarios and when processing structured
data with headers.
Author
List all Java tutorials.