In  this tutorial, we will see “What is a Skip() method in Java 8 Stream and how we can use it?” skip stream java 8

Skip() method in Java Stream...!!! Share on X

Streams SKIP() Method

Syntax & Description

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.

This is a stateful intermediate operation.

Syntax: Stream<T> skip(long n)

How to use it – an Example?

/**
 * Using Skip method as name suggest, Skip first 5 integers and print the rest
 * @author Deepak Verma
 *
 */

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class Skip_Java8Stream_Example {

	public static void main(String args[]) {

		List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

		// Get a stream of the numbers
		Stream<Integer> stream = numbers.stream();

		// Skip the first 5 elements and print the remaining elements
		stream.skip(5)
		      .forEach(System.out::println);

	}
}

 

Output:

6
7
8
9
10

stream to array java

skip method java 8 stream

Java 8 – Stream Skip() method with Example Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts