Java Stream skip() Method

The skip() method in Java, part of the java.util.stream.Stream interface, is used to skip the first n elements of the stream. This method is useful when you need to bypass a specific number of elements in the stream and continue processing the remaining elements.

Table of Contents

  1. Introduction
  2. skip() Method Syntax
  3. Understanding skip()
  4. Examples
    • Basic Usage
    • Using skip() in Combination with Other Stream Operations
  5. Real-World Use Case
  6. Conclusion

Introduction

The skip() method returns a stream consisting of the remaining elements of the original stream after discarding the first n elements. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

skip() Method Syntax

The syntax for the skip() method is as follows:

Stream<T> skip(long n)

Parameters:

  • n: The number of leading elements to skip.

Returns:

  • A new Stream consisting of the remaining elements of the original stream after discarding the first n elements.

Throws:

  • This method does not throw any exceptions.

Understanding skip()

The skip() method allows you to discard the first n elements from a stream and continue processing the remaining elements. This is useful for scenarios where you need to ignore a certain number of elements and focus on the rest, such as pagination or removing headers from a dataset.

Examples

Basic Usage

To demonstrate the basic usage of skip(), we will create a Stream of integers and use skip() to skip the first 3 elements.

Example

import java.util.stream.Stream;

public class SkipExample {
    public static void main(String[] args) {
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

        // Use skip() to skip the first 3 elements
        Stream<Integer> skippedStream = stream.skip(3);

        // Print the remaining elements
        skippedStream.forEach(System.out::println);
    }
}

Output:

4
5
6

Using skip() in Combination with Other Stream Operations

This example shows how to use skip() in combination with other stream operations, such as filtering and limiting.

Example

import java.util.stream.Stream;

public class SkipWithFilterExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("apple", "banana", "cherry", "date", "elderberry", "fig", "grape");

        // Use skip() to skip the first 2 elements and filter to keep elements with length greater than 4
        Stream<String> filteredStream = stream.skip(2)
                                              .filter(s -> s.length() > 4);

        // Print the remaining elements
        filteredStream.forEach(System.out::println);
    }
}

Output:

cherry
elderberry
grape

Real-World Use Case

Implementing Pagination

In real-world applications, the skip() method can be used to implement pagination by skipping elements and limiting the number of elements processed.

Example

import java.util.stream.Stream;

public class PaginationExample {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("apple", "banana", "cherry", "date", "elderberry", "fig", "grape");

        int page = 2;
        int pageSize = 3;

        // Use skip() to skip elements of previous pages and limit to get the elements of the current page
        Stream<String> pageStream = stream.skip((page - 1) * pageSize)
                                          .limit(pageSize);

        // Print the elements of the current page
        pageStream.forEach(System.out::println);
    }
}

Output:

date
elderberry
fig

Conclusion

The Stream.skip() method is used to skip the first n elements of a stream and continue processing the remaining elements. This method is particularly useful for scenarios such as pagination or removing headers from a dataset. By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, focusing on the elements that are relevant to your needs.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare