The methods of skip() and limit() in Stream and their combination

1 Introduction

This article will explain two methods in Java 8 Stream: skip() and limit(). These two methods are commonly used by Stream. They will not only be used frequently, but also can be combined, and can realize some small functions, such as subList and paging.

2 skip() method

As the name implies, the skip() method is used to skip the first n elements and then return a new stream, as shown in the figure:

Let's look at the code:

List<Integer> result = Stream.of(1, 2, 3, 4, 5, 6)
  .skip(4)
  .collect(Collectors.toList());
List<Integer> expected = asList(5, 6);
assertEquals(expected, result);

Four cases of parameter n of method skip():

(1) When n < 0, throw the IllegalArgumentException exception;

(2) When n=0, it doesn't skip any elements and returns intact;

(3) When 0 < n < length, skip n elements and return the stream containing the remaining elements;

(4) When n > = length, skip all elements and return an empty stream.

3. Limit() method

For the limit() method, it is used to limit the number of elements in the stream, that is, take the first n elements and return a new stream, as shown in the figure:

The code is as follows:

List<Integer> result = Stream.of(1, 2, 3, 4, 5, 6)
  .limit(4)
  .collect(Collectors.toList());
List<Integer> expected = asList(1, 2, 3, 4);
assertEquals(expected, result);

There are four cases of parameter n of method limit():

(1) When n < 0, throw the IllegalArgumentException exception;

(2) When n=0, no element is taken and an empty stream is returned;

(3) When 0 < n < length, take the first n elements and return a new stream;

(4) When n > = length, take all elements and return them intact.

4 operation of infinite flow

Stream is divided into finite stream and infinite stream. In the previous examples, we use finite stream. Unlike Java collection classes, stream can be infinite. For infinite streams, skip() and limit() show great differences. First, the code:

Stream.iterate(1, i -> i + 1)
  .filter(num -> (num & (num - 1)) == 0)
  .limit(10)
  .forEach(System.out::println);
System.out.println("----------------");
Stream.iterate(1, i -> i + 1)
  .filter(num -> (num & (num - 1)) == 0)
  .skip(10)
  .forEach(System.out::println);

After execution, it is found that limit() can convert infinite flow into finite flow, so we can also think of it as a short-circuit operation. skip() doesn't work. No matter how many elements you skip, there will always be a steady stream of elements that can't converge.

The result of the above code is:

The first ten n-th power values of 2 are output through limit():
1, 2, 4, 8, 16, 32, 64, 128, 256, 512

skip() skips the first 10 and continues to output, but it will continue to execute (there will be int overflow):

1024, 2048, 4096, 8192, 16384, 32768...

5 combined application

In addition to their respective functions, we can realize other functions through combination.

5.1 substitution with subList

Collection classes such as List have a subList() method, which can intercept a part of the List. This function can also be obtained by combining skip() and limit(), as shown in the following code:

List<Integer> list = asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> expected = list.subList(3, 7);

List<Integer> result = list.stream()
  .skip(3)
  .limit(7 - 3)
  .collect(Collectors.toList());
assertEquals(expected, result);

Convert subList(startIndex, endIndex) to skip (StartIndex) limit(endIndex - startIndex).

5.2 paging

Paging can be performed by combining skip() and limit(), as shown in the following code:

int pageSize = 10;
int pageIndex = 7;

List<Integer> expected = asList(61, 62, 63, 64, 65, 66, 67, 68, 69, 70);
List<Integer> result = Stream.iterate(1, i -> i + 1)
  .skip((pageIndex - 1) * pageSize)
  .limit(pageSize)
  .collect(Collectors.toList());

assertEquals(expected, result);

The above code example obtains the seventh page of data, and the size of each page is 10.

6 Summary

This paper introduces two common methods in the Stream interface of Java 8: skip() and limit(), which are relatively simple and easy to understand. It also introduces how to combine them. It should be noted that if the Stream is too large or infinite, be careful that skip() will have performance problems.

Keywords: Java

Added by abhi_madhani on Wed, 19 Jan 2022 08:18:08 +0200