Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Stream.skip: practical reference

Stream.skip: practical reference: Stream.skip(n) is a lazy stateful intermediate operation that discards the first n elements in encounter order and exposes the remainder.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

Stream.skip(...)

Stream.skip(n) is a lazy stateful intermediate operation that discards the first n elements in encounter order and exposes the remainder.

Use skip to advance through an ordered sequence or paginate a stable source, remembering that the skipped prefix still has to be traversed.

A large n can be expensive on an ordered parallel stream; unordered may improve speed but no longer defines which n elements are discarded.

A negative n throws IllegalArgumentException, zero discards nothing, and n at least as large as the stream size yields an empty stream.

skip(2) discards the first two elements of the ordered 1, 2, 3 stream; toList retains only 3 and prints exactly [3].

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var result = java.util.stream.Stream.of(1, 2, 3).skip(2).toList();
        System.out.println(result);
    }
}

Result

[3]

skip(2) discards the first two elements of the ordered 1, 2, 3 stream; toList retains only 3 and prints exactly [3].

Sources