Java Stream limit
last modified May 25, 2025
This article explores the limit method in Java Streams, showing how to efficiently restrict the number of elements processed in a stream.
The limit method is an intermediate operation that truncates a stream to ensure it contains no more than a specified number of elements. This feature is especially useful when working with infinite streams or when extracting a manageable subset from a large dataset.
Basic limit syntax
The limit
method restricts the number of elements in a stream to a
specified maximum, returning a new stream with at most that many elements.
$ java Main.java Stream<T> limit(long maxSize)
The parameter maxSize is the maximum number of elements the stream should contain. If the stream has fewer elements than maxSize, it remains unchanged.
Limiting finite streams
You can use limit
with finite streams to process only the first n
elements and discard the rest.
void main() { Stream.of("A", "B", "C", "D", "E", "F") .limit(3) .forEach(System.out::println); }
This example limits the stream to the first 3 elements ("A", "B", "C"), discarding the rest.
$ java Main.java A B C
Working with infinite streams
The limit
method is essential for making infinite streams finite
and manageable by truncating them to a fixed number of elements.
void main() { Stream.generate(() -> (int)(Math.random() * 100)) .limit(5) .forEach(System.out::println); }
This example generates an infinite stream of random numbers but limits it to 5 elements, making it possible to process.
Combining with skip for pagination
Combining skip
and limit
enables efficient pagination
by selecting a specific range of elements from a stream.
void main() { int pageSize = 4; int pageNumber = 1; // zero-based IntStream.rangeClosed(1, 20) .skip(pageNumber * pageSize) .limit(pageSize) .forEach(System.out::println); }
This common pattern skips elements from previous pages and limits to the current page size (elements 5-8 in this case).
$ java Main.java 5 6 7 8
Combining with filter
The order of limit
and filter
operations determines
which elements are included, as limit applies after filtering.
void main() { IntStream.rangeClosed(1, 100) .filter(n -> n % 3 == 0) .limit(5) .forEach(System.out::println); }
This example finds numbers divisible by 3 but limits the result to the first 5 matches (3, 6, 9, 12, 15).
Top N results
A common use case for limit
is retrieving the top N results from a
processed stream, such as the highest values or most relevant items.
record Product(String name, BigDecimal price) { } void main() { Stream.of( new Product("Laptop", new BigDecimal("999.99")), new Product("Phone", new BigDecimal("699.99")), new Product("Tablet", new BigDecimal("349.99")), new Product("Monitor", new BigDecimal("249.99")), new Product("Keyboard", new BigDecimal("49.99")) ) .sorted((p1, p2) -> p2.price().compareTo(p1.price())) // BigDecimal sorting .limit(3) .forEach(p -> System.out.println(p.name() + ": $" + p.price())); }
This common pattern sorts products by price (descending) and limits to the top 3 most expensive items.
$ java Main.java Laptop: $999.99 Phone: $699.99 Tablet: $349.99
Source
Java Stream limit documentation
In this article we have explored the Java Stream limit
method. It's
an essential tool for controlling stream size, enabling efficient processing of
infinite streams and large datasets by restricting the number of elements
processed.
Author
List all Java tutorials.