DEV Community

realNameHidden
realNameHidden

Posted on

3 1 1 1 1

Java 8 Stream API limit() and skip() methods

In Java 8, the Stream API provides limit() and skip() methods for controlling the number of elements in a stream.

limit(n): Limits the stream to the first n elements.

skip(n): Skips the first n elements and processes the rest.

Here’s an example demonstrating both:

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

public class StreamLimitSkipExample {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // Using limit() to get the first 5 elements
        List<Integer> limitedList = numbers.stream()
                .limit(5)
                .collect(Collectors.toList());
        System.out.println("First 5 elements: " + limitedList);

        // Using skip() to skip the first 5 elements and get the rest
        List<Integer> skippedList = numbers.stream()
                .skip(5)
                .collect(Collectors.toList());
        System.out.println("After skipping first 5 elements: " + skippedList);

        // Combining skip() and limit() to get elements from 4th to 7th positions
        List<Integer> limitedAndSkippedList = numbers.stream()
                .skip(3)     // skip first 3 elements (index starts at 0)
                .limit(4)    // then take the next 4 elements
                .collect(Collectors.toList());
        System.out.println("Elements from 4th to 7th positions: " + limitedAndSkippedList);
    }
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

Using limit(5): This limits the stream to the first 5 elements, resulting in [1, 2, 3, 4, 5].

Using skip(5): This skips the first 5 elements and collects the rest, resulting in [6, 7, 8, 9, 10].

Combining skip(3) and limit(4): First, it skips the first 3 elements, then limits to the next 4, resulting in elements from positions 4 to 7: [4, 5, 6, 7].

Output:

First 5 elements: [1, 2, 3, 4, 5]
After skipping first 5 elements: [6, 7, 8, 9, 10]
Elements from 4th to 7th positions: [4, 5, 6, 7]

Enter fullscreen mode Exit fullscreen mode

This approach is useful for handling pagination or extracting specific ranges in a collection.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay