Close

Java 8 Streams - Stream.limit Examples

Java 8 Streams Java Java API 


Interface:

java.util.stream.Stream

java.lang.AutoCloseableAutoCloseablejava.util.stream.BaseStreamBaseStreamjava.util.stream.StreamStreamLogicBig

Method:

Stream<T> limit(long maxSize)

This intermediate operation returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

This is a short-circuiting stateful operation.


Examples


This example shows how to apply the intermediate stream operation 'limit()'. This method marks a stream to give reduced size results at the terminal stage.

package com.logicbig.example;

import java.util.Arrays;
import java.util.stream.IntStream;

public class LimitExample {

public static void main (String[] args) {
int[] ints = {1, 2, 3, 4, 5, 6};

System.out.printf("Source: %s%n", Arrays.toString(ints));
System.out.println("Finding even numbers.");
runWithoutLimit(Arrays.stream(ints));
//Note: creating and passing new stream because it
// cannot be reused after a terminal operation is called.
runWithLimit(Arrays.stream(ints));
}

private static void runWithoutLimit (IntStream stream) {
System.out.println("Running without limit()");

//filter even numbers
stream.filter(i -> i % 2 == 0)
.forEach(System.out::println);
}

private static void runWithLimit (IntStream stream) {
System.out.println("Running with limit(2)");

//filter even numbers
stream.filter(i -> i % 2 == 0)
.limit(2)
.forEach(System.out::println);
}
}

Output

Source: [1, 2, 3, 4, 5, 6]
Finding even numbers.
Running without limit()
2
4
6
Running with limit(2)
2
4
Original Post




Limiting an infinite stream.

package com.logicbig.example;

import java.util.stream.Stream;

public class LimitingInfiniteStream {
public static void main (String[] args) {
Stream<Integer> stream = Stream.iterate(1, i -> i + 1);
stream.filter(i -> i % 2 == 0)
.limit(5)
.forEach(System.out::println);
}
}

Output

2
4
6
8
10

This example limit the stream first before applying filter criteria.

package com.logicbig.example;

import java.util.stream.Stream;

public class LimitingInfiniteStream2 {
public static void main (String[] args) {
Stream<Integer> stream = Stream.iterate(1, i -> i + 1);
stream.limit(5)
.filter(i -> i % 2 == 0)
.forEach(System.out::println);
}
}

Output

2
4
Original Post




Ordered vs Unordered streams

This example shows the difference in performance of limit operation when using parallel ordered vs parallel unordered streams.

package com.logicbig.example;

import java.util.stream.IntStream;

public class LimitExample {

public static void main (String[] args) {

PerformanceTestUtil.runTest("unordered parallel stream limit test", () -> {
IntStream stream = IntStream.range(0, 1000000000);
stream.unordered()
.parallel()
.filter(i -> i % 2 == 0)
.limit(100000000)
.count();
});

PerformanceTestUtil.runTest("ordered parallel stream limit test", () -> {
IntStream stream = IntStream.range(0, 1000000000);
stream.parallel()
.filter(i -> i % 2 == 0)
.limit(100000000)
.count();
});
}
}

Output

unordered parallel stream limit test time taken: 571.0 milliseconds
ordered parallel stream limit test time taken: 1.628 seconds
Original Post




See Also