Friday, November 21, 2025
HomeLanguagesJavaIntStream skip() in Java

IntStream skip() in Java

IntStream 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 : IntStream skip() is a stateful intermediate operation i.e, it may incorporate state from previously seen elements when processing new elements.

Syntax :

IntStream skip(long n)

Parameter : IntStream is a sequence of primitive int-valued elements. This is the int 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 IntStream skip() function
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream of numbers [5, 6, .. 11]
        IntStream stream = IntStream.range(5, 12);
  
        // Using skip() to skip first 4 values in range
        // and displaying the rest of elements
        stream.skip(4).forEach(System.out::println);
    }
}


Output :

9
10
11

Example 2 :




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


Output :

10
11
9
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11997 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS