Home  >  Java 8

Java Stream skip()

By Arvind Rai, June 06, 2020
This page will walk through Java Stream.skip method example. The skip method returns a stream consisting of the remaining elements of this stream after skipping the first given elements of the stream.
Find the skip method declaration from Java doc.
Stream<T> skip(long n) 
Parameters: Pass the number of leading elements to skip.
Returns: The method returns a new stream skipping the elements.
Throws: If we pass negative number, it throws IllegalArgumentException.

1. The skip method is used to create a new stream from a source stream skipping the given number of elements from start.
2. The skip method is costly for ordered parallel pipelines, especially for bigger number of n.
3. The skip method is cheap operation for sequential stream pipelines.
4. If number of elements to skip, n, is equal or greater than the number of elements in stream then the output will be empty stream.
5. The skip is stateful intermediate operation.

stateful intermediate operation
Intermediate operations are divided into stateless and stateful operations. Stateless operations retain no state from previously seen element when processing a new element such as filter and map. Stateful operations may incorporate state from previously seen elements when processing new elements such as skip and limit methods.

skip() Examples

Find some examples of skip method.
Example-1:
In this example we have a stream of integer elements and we are using skip method to get new stream in which the first three elements will be discarded.
Stream.of(10, 20, 30, 40, 50)
  .skip(3)
  .forEach(System.out::println); 
The output will be 40, 50.
Example-2:
In this example we have a stream of integers. First we filter it and extract even number and then skip by two in final result.
Stream.of(11,12,13,14,15,16,17,18)
  .filter(n -> n % 2 == 0) //12,14,16,18
  .skip(2) //16,18
  .forEach(System.out::println); 
The output will be 16,18.
Example-3:
Here we have a stream of alphabets. First we sort it and then skip by 2 elements.
Stream.of("C", "B", "D", "F", "E", "A")
  .sorted()// A,B,C,D,E,F
  .skip(2) //C,D,E,F
  .forEach(System.out::println); 
The output will be C,D,E,F.
Example-4:
In this example we will skip more elements than the number of elements in the stream. The output will be empty stream.
Stream.of("A", "B", "C", "D")
  .skip(5)
  .forEach(System.out::println); 
This will print nothing as there is no element in the stream after skip.

skip() vs limit()

The skip method discards the given number of elements from start and returns the new stream with remaining number of elements up to the last element whereas limit method returns the new stream up to the given number of elements starting from first element. The skip and limit both are the intermediate operation and returns new stream.
Example-1:
Here we have a list of integers. We will run limit and skip both on source stream. See the result.
SkipLimitDemo.java
package com.concretepage;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SkipLimitDemo {
  public static void main(String[] args) {
	List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);

	List<Integer> afterLimit = list.stream().limit(4).collect(Collectors.toList());

	List<Integer> afterSkip = list.stream().skip(4).collect(Collectors.toList());

	System.out.println("Limit: " + afterLimit);
	System.out.println("Skip: " + afterSkip);
  }
} 
Output
Limit: [1, 2, 3, 4]
Skip: [5, 6, 7, 8] 
Example-2:
In this example we will run first skip on the source element and then limit on the stream returned by skip method.
Stream.of(1,2,3,4,5,6,7,8)
	.skip(2) //3,4,5,6,7,8
        .limit(2) //3,4
	.forEach(System.out::println); 
The output will be 3,4.

Reference

Java doc: Stream
POSTED BY
ARVIND RAI
ARVIND RAI








©2020 concretepage.com | Privacy Policy | Contact Us