Core Java

Java 8 Stream API – limit() & skip() Example

Hello. In this tutorial, we will explore the Stream API: limit() and skip() methods introduced in Java 8.

1. Introduction

Java 8 stream limit

Before diving deep into the practice stuff let us understand the methods we will be covering in this tutorial.

  • The skip() method discards the first n elements of a stream. n cannot be a negative number and if it is higher than the size of a stream the method will return an empty stream
  • The limit() method retrieves the number of elements from the stream truncated to be no longer than the given maximum size. It is an intermediate operation

2. Practice

Let us dive into some practice stuff from here and I am assuming that you already have the Java 1.8 or greater installed in your local machine. I am using JetBrains IntelliJ IDEA as my preferred IDE. You’re free to choose the IDE of your choice.

2.1 Understanding limit() method

Create a java file in the com.java8 package and add the following code. The class will show the limit() method implementation in different ways.

LimitDemo.java

package com.java8;

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

public class LimitDemo {

    // limit() method retrieves the number of elements from the stream truncated to be no longer than given maximum
    // size. it is a intermediate operation
    // syntax - Stream<T> limit(long maxSize);

    private static void method1() {
        // creating a numbers list
        List<Integer> numbers = new ArrayList<>();
        for (int i = 1; i < 51; i++) {
            numbers.add(i);
        }

        int maxSize = 10;
        // taking only first 10 values from stream and converting them into list
        List<Integer> limit10 = numbers.stream()
                .limit(maxSize)
                .collect(Collectors.toList());
        // printing
        System.out.println(limit10);
    }

    // driver code
    public static void main(String[] args) {
    	System.out.println("-- Streams limit() method --\n");
        method1();
    }
}

Run the file as a java application and if everything goes well the following output will be logged in the IDE console.

Console output

-- Streams limit() method --

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

2.2 Understanding skip() method

Create a java file in the com.java8 package and add the following code. The class will show the skip() method implementation in different ways.

SkipDemo.java

package com.java8;

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

public class SkipDemo {

    // skip() method discards the first n elements of a stream. 'n' cannot be a negative number and if it is higher
    // than the size of stream, the skip() method will return an empty stream
    // syntax - Stream<T> skip(long n);

    private static void method1() {
        // creating a numbers list
        List<Integer> numbers = new ArrayList<>();
        for (int i=1; i<16; i++) {
            numbers.add(i);
        }

        List<Integer> skip10 = numbers.stream()
                .skip(10)
                .collect(Collectors.toList());
        // printing
        System.out.println(skip10);
    }

    // driver code
    public static void main(String[] args) {
    	System.out.println("-- Streams skip() method --\n");
        method1();
    }
}

Run the file as a java application and if everything goes well the following output will be logged in the IDE console.

Console output

-- Streams skip() method --

[11, 12, 13, 14, 15]

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned limit() and skip() methods introduced in java8 programming along with the implementation. You can download the source code from the Downloads section.

4. Download the Project

This was a tutorial on learning and implementing the limit() and skip() methods in java8 programming.

Download
You can download the full source code of this example here: Java 8 Stream API – limit() & skip() Example

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button