Java Streams - IntStream limit(long maxSize) example








IntStream limit(long maxSize) returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

Syntax

limit has the following syntax.

IntStream limit(long maxSize)

Example

The following example shows how to use limit.

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 4);
    i.limit(3)
     .forEach(System.out::println);
  }
}

The code above generates the following result.