A IntStream method limit() is a shortcurcuit operation that limits the stream elements upto the specified maxSize value. It returns a stream that includes an elements of this stream which are truncated to be no longer than maxSize in length.
1 IntStream limit(long maxSize)
maxSize : It specifies the number of elements the stream should be limited to.
return : It returns new stream that includes specified length elements.
1 package com.java;
2
3 import java.util.stream.IntStream;
4
5 public class Main {
6
7 public static void main(String[] args) {
8
9
10 IntStream stream1 = IntStream.generate(() -> (int) (Math.random() * 100));
11
12
13 IntStream res = stream1.limit(5);
14
15 System.out.println("A stream elements are :");
16 res.forEach(System.out::println);
17 }
18 }
In the above example, a stream object created using generate() method by specifying a generator function which will generate random number between 0 to 100. A stream method limit() called by passing a maxSize as 5 which will limit the stream to generate 5 random number. A resulting stream object elements print using forEach() method.
1 A stream elements are :
2 83
3 30
4 0
5 71
6 47
7
IntStream method limit() with intermediate operator
IntStream method limit() with intermediate operator
1 package com.java;
2
3 import java.util.stream.IntStream;
4
5 public class Main {
6
7 public static void main(String[] args) {
8
9
10 IntStream stream1 = IntStream.generate(() -> (int) (Math.random() * 100));
11
12
13 IntStream res = stream1.filter(ele -> ele % 5 ==0)
14 .limit(5);
15
16 System.out.println("A stream elements are :");
17 res.forEach(System.out::println);
18 }
19 }
In the above example, a stream method filter() called that validates whether a number is divisible by 5 and returns a resulting stream object. A stream method limit() called that limits a stream elements to 5 elements which are divisible by 5 and print resulting stream elements.
1 A stream elements are :
2 65
3 55
4 15
5 0
6 65
limit() maxSize greater than stream element count
limit() maxSize greater than stream element count
1 package com.java;
2
3 import java.util.stream.IntStream;
4
5 public class Main {
6
7 public static void main(String[] args) {
8
9 IntStream stream1 = IntStream.of(2, 5, 3, 5, 6, 7 );
10
11
12 IntStream res = stream1.limit(15);
13
14 System.out.println("A stream elements are :");
15 res.forEach(System.out::println);
16 }
17 }
In the above example, a stream method limit() called by specifying a maxSize value as 15 which is greater than the count of stream elements. Hence, it includes all elements of stream object and print.
1 A stream elements are :
2 2
3 5
4 3
5 5
6 6
7 7
Note :While limit() is generally a cheap operation on sequential stream pipelines, it can be quite expensive on ordered parallel pipelines, especially for large values of maxSize, since limit(n) is constrained to return not just any n elements, but the first n elements in the encounter order.
Using an unordered stream source (such as generate(IntSupplier)) or removing the ordering constraint with BaseStream.unordered() may result in significant speedups of limit() in parallel pipelines, if the semantics of your situation permit. If consistency with encounter order is required, and you are experiencing poor performance or memory utilization with limit() in parallel pipelines, switching to sequential execution with sequential() may improve performance.
Related options for your search