Stream skip() method in Java with examples

Prerequisite : Streams in java
The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N) is constrained to skip the first N elements in the encounter order and not just any n elements.
Note : If a stream contains less than N elements, then an empty stream is returned.

Syntax :

Stream<T> skip(long N)

Where N is the number of elements to be skipped
and this function returns new stream as output.

Exception : If the value of N is negative, then IllegalArgumentException is thrown by the function.



Example 1 : Implementation of skip function.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for skip() function
import java.util.*;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Creating a list of integers
        List<Integer> list = new ArrayList<Integer>();
  
        // adding elements in the list
        list.add(-2);
        list.add(0);
        list.add(2);
        list.add(4);
        list.add(6);
        list.add(8);
        list.add(10);
        list.add(12);
        list.add(14);
        list.add(16);
  
        // setting the value of N as 4
        int limit = 4;
        int count = 0;
        Iterator<Integer> it = list.iterator();
  
        // Iterating through the list of integers
        while (it.hasNext()) {
            it.next();
            count++;
  
            // Check if first four i.e, (equal to N)
            // integers are iterated.
            if (count <= limit) {
  
                // If yes then remove first N elements.
                it.remove();
            }
        }
  
        System.out.print("New stream is : ");
  
        // Displaying new stream
        for (Integer number : list) {
            System.out.print(number + " ");
        }
    }
}

chevron_right


Output :

New stream is : 6 8 10 12 14 16 

Application :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for skip() function
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.List;
 class gfg{
       
     // Function to skip the elements of stream upto given range, i.e, 3
     public static Stream<String> skip_func(Stream<String> ss, int range){
         return ss.skip(range);
     }
       
     // Driver code
     public static void main(String[] args){
           
         // list to save stream of strings
         List<String> arr = new ArrayList<>();
           
         arr.add("geeks");
         arr.add("for");
         arr.add("geeks");
         arr.add("computer");
         arr.add("science");
          
         Stream<String> str = arr.stream();
           
         // calling function to skip the elements to range 3
         Stream<String> sk = skip_func(str,3);
         sk.forEach(System.out::println);
     }
 }

chevron_right


Output :

computer
science

Difference between limit() and skip() :

  1. The limit() method returns a reduced stream of first N elements but skip() method returns a stream of remaining elements after skipping first N elements.
  2. limit() is a short-circuiting stateful intermediate operation i.e, when processed with an infinite input, it may produce a finite stream as a result without processing the entire input but skip() is a stateful intermediate operation i.e, it may need to process the entire input before producing a result.


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.