IntStream limit() method in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 28-Mar-2019 14:44:42
Advertisements