Java Stream skip() with examples

 197 total views,  2 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream skip() with examples.

skip() is a part of Stream pipeline and an intermediate operation. It belongs to the Stream interface.

  1. Java stream skip operation allows us to skip first N elements based on the input N.It generated a new stream by discarding the first N elements
  2. When the length of the stream is less than N, then skip() will return an empty Stream.

skip() Method description

Stream<T> skip(long n)

As per official documentation

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.

Since skip() is an intermediate operation it will have two characteristics.

  1. skip() is lazy loading operation. This means data will only be skipped when a terminal operation is invoked on the stream.
  2. skip() operation on a stream will always create a new stream output.

Let us now see a few examples of Java 8 stream skip()

Usual way of skipping N elements using for-loop

import java.util.ArrayList;
import java.util.List;

public class StaticReference
{
    public static void main(String[] args)
    {
        int N = 5; //skip first 5 elements
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("orange");
        list.add("banana");
        list.add("Guava");
        list.add("plums");
        list.add("peach");
        list.add("cherry");
        list.add("jackfruit");

        for (int i = 0; i < list.size(); i++)
        {
            if (i < N) continue;
            System.out.println(list.get(i));
        }
     }
}

Output

peach
cherry
jackfruit

Java stream skip() operation to skip N elements

import java.util.ArrayList;
import java.util.List;

public class StaticReference
{
    public static void main(String[] args)
    {
        List<String> list = new ArrayList<>();
        list.add("apple");
        list.add("orange");
        list.add("banana");
        list.add("Guava");
        list.add("plums");
        list.add("peach");
        list.add("cherry");
        list.add("jackfruit");
       
        //skip 5 elements
        list.stream().skip(5).forEach(System.out::println);
    }

}

Output

peach
cherry
jackfruit

Conclusion

Java Stream skip() operation is mainly used for skipping the first N stream elements. skip() will always create a new stream and it is lazily evaluated.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.