IntStream limit() in Java

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

Note : IntStream limit() is a short-circuiting stateful intermediate operation i.e, when processed with an infinite input, it may produce a finite stream as a result without processing the entire input.

Syntax :



IntStream limit(long maxSize)

Parameters :

  1. IntStream : A sequence of primitive int-valued elements. This is the int primitive specialization of Stream.
  2. maxSize : The number of elements the stream should be limited to.

Return Value : The function returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

Exception : The function throws IllegalArgumentException if maxSize is negative.

Example 1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream limit
// (long maxSize)
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.of(2, 4, 6, 8, 10);
  
        // Using IntStream limit(long maxSize) to
        // get a stream consisting of the elements of
        // this stream, truncated to be no longer
        // than maxSize in length.
        stream.limit(3).forEach(System.out::println);
    }
}

chevron_right


Output :

2
4
6

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream limit
// (long maxSize)
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream of numbers [5, 6, .. 11]
        IntStream stream = IntStream.range(5, 12);
  
        // Using IntStream limit(long maxSize) to
        // get a stream consisting of the elements of
        // this stream, truncated to be no longer
        // than maxSize in length.
        stream.limit(4).forEach(System.out::println);
    }
}

chevron_right


Output :

5
6
7
8

Example 3 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for IntStream limit
// (long maxSize)
import java.util.*;
import java.util.stream.IntStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an IntStream
        IntStream stream = IntStream.iterate(4, num -> num + 2);
  
        // Using IntStream limit(long maxSize) to
        // get a stream consisting of the elements of
        // this stream, truncated to be no longer
        // than maxSize in length.
        stream.limit(4).forEach(System.out::println);
    }
}

chevron_right


Output :

4
6
8
10

Difference between IntStream limit() and IntStream skip() :

  1. The limit() method returns a reduced stream of first maxSize elements but skip() method returns a stream of remaining elements after skipping first maxSize elements.
  2. limit() is a short-circuiting stateful intermediate operation i.e, when processed with an infinite input, it may produce a finite stream as a result without processing the entire input but skip() is a stateful intermediate operation i.e, it may need to process the entire input before producing a result.


My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.