Friday, April 3, 2026
HomeLanguagesJavaLongStream skip() in Java

LongStream skip() in Java

LongStream skip(long n) returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream. If this stream contains fewer than n elements then an empty stream will be returned.

Note : LongStream skip() is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.

Syntax :

LongStream skip(long n)

Parameter : LongStream is a sequence of primitive long-valued elements. This is the long primitive specialization of Stream. n is the number of leading elements to skip.

Return Value : The function returns the new stream after discarding first n elements.

Exception : The function throws IllegalArgumentException if n is negative.

Example 1 :




// Java code for LongStream skip() function
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream of numbers [5, 6, .. 11]
        LongStream stream = LongStream.range(5L, 12L);
  
        // Using skip() to skip first 4 values in range
        // and displaying the rest of elements
        stream.skip(4).forEach(System.out::println);
    }
}


Example 2 :




// Java code for LongStream skip() function
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream [5, 6, .. 11]
        LongStream stream = LongStream.range(5L, 12L);
  
        // Using parallel skip() to skip first
        // 4 values in range and displaying the
        // rest of elements
        stream.parallel().skip(4).forEach(System.out::println);
    }
}


RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32512 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6885 POSTS0 COMMENTS
Nicole Veronica
12006 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12100 POSTS0 COMMENTS
Shaida Kate Naidoo
7015 POSTS0 COMMENTS
Ted Musemwa
7259 POSTS0 COMMENTS
Thapelo Manthata
6971 POSTS0 COMMENTS
Umr Jansen
6960 POSTS0 COMMENTS