IntStream limit() method in Java


The limit() method of the IntStream class is used to return a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length. Here, maxSize is the parameter.

The syntax is as follows

IntStream limit(long maxSize)

Here, the maxSize parameter is the count of elements the stream is limited to.

At first, an IntStream is created with the range() method to set a sequential order of elements

IntStream intStream = IntStream.range(20, 40);

Now, use the limit() method, whose parameter is the maxSize i.e. the count of elements the stream is limited to

intStream.limit(8)

The following is an example to implement IntStream limit() method in Java

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(20, 40);
      intStream.limit(8).forEach(System.out::println);
   }
}

Output

20
21
22
23
24
25
26
27

Updated on: 30-Jul-2019

882 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements