Java IntStream rangeClosed: Inclusive Range Generation
Learn how to use Java IntStream.rangeClosed to generate inclusive integer ranges, with syntax, examples, and edge cases to avoid common mistakes.

IntStream.rangeClosed(int startInclusive, int endInclusive) returns a sequential, ordered IntStream of integers from startInclusive through endInclusive, including both endpoints and incrementing by 1. It is part of the java.util.stream.IntStream interface and is convenient for loops, index generation, and creating test data.
IntStream.rangeClosed Syntax and Basic Behavior
The signature is:
static IntStream rangeClosed(int startInclusive, int endInclusive)The returned stream contains the integers from startInclusive through endInclusive. For example:
IntStream.rangeClosed(1, 5)
.forEach(System.out::println);The terminal operation prints the values 1 through 5, each on a separate line. The stream is sequential and ordered, and values are generated on demand. The method does not materialize an array.
rangeClosed vs range: Inclusive vs Exclusive Endpoint
The sibling method IntStream.range(int startInclusive, int endExclusive) excludes the upper bound. Choose between them based on whether the endpoint should be included.
| Method | Endpoint behavior | Example (1, 5) | Use case |
|---|---|---|---|
range | Exclusive | 1, 2, 3, 4 | Zero-based indexing, array length |
rangeClosed | Inclusive | 1, 2, 3, 4, 5 | Inclusive numeric ranges, loop counters using <= |
For an array of length n, range(0, n) is natural because valid indices are 0 through n - 1. If you need to include the upper bound, such as iterating from 0 through 10 inclusive, rangeClosed(0, 10) is correct. Mixing the two methods is a common source of off-by-one errors.
Practical Usage Patterns for rangeClosed
Beyond simple printing, rangeClosed is often used with other stream operations. To sum a range:
int total = IntStream.rangeClosed(1, 100).sum();To map each integer to a derived value, such as squares:
List<Integer> squares = IntStream.rangeClosed(1, 10)
.map(n -> n * n)
.boxed()
.collect(Collectors.toList());It also works well for generating indices for list access:
List<String> items = Arrays.asList("a", "b", "c");
IntStream.rangeClosed(0, items.size() - 1)
.forEach(i -> System.out.println(i + ": " + items.get(i)));rangeClosed always increments by 1. There is no overload for a custom step size. For other steps, use IntStream.iterate or a traditional for loop.
Performance Considerations: Lazy Evaluation and Overhead
IntStream.rangeClosed is lazy: no values are computed until a terminal operation is invoked, so you can chain intermediate operations without materializing the entire range. For example, IntStream.rangeClosed(1, 1_000_000).filter(n -> n % 2 == 0).limit(10) processes only enough values to satisfy the limit.
Streams add some overhead compared with a simple for loop. In tight, performance-critical loops over millions of elements, a traditional loop may be faster because it avoids stream abstraction and boxing when converting to Stream<Integer>. For typical business logic the difference is usually negligible, but if you are processing very large ranges in a hot path, measure both approaches.
Memory usage is also constant: rangeClosed does not pre-allocate an array, unlike manually building a list of integers.
Common Mistakes and Edge Cases
Do not assume rangeClosed supports a custom step. It only increments by 1. If you need even numbers, use filter or iterate.
If startInclusive is greater than endInclusive, the stream is empty. For example, IntStream.rangeClosed(5, 1) produces no elements. If startInclusive equals endInclusive, the stream contains exactly one element. Java does not generate descending ranges with this method; use iterate or a loop for descending sequences.
Overflow is also possible. If endInclusive is Integer.MAX_VALUE, the range reaches the upper int bound, and arithmetic operations such as sum() can overflow. Be mindful of range size when performing calculations.
When to Use rangeClosed vs Other Range Alternatives
rangeClosed is ideal for fixed, inclusive integer ranges. Alternatives include:
IntStream.rangewhen the endpoint is exclusive.IntStream.iteratefor custom steps or unbounded sequences.- A traditional
forloop when you need mutable loop state or a step other than 1.
For example, to iterate from 1 to 10 with a step of 2:
for (int i = 1; i <= 10; i += 2) { ... }Or with iterate:
IntStream.iterate(1, i -> i <= 10, i -> i + 2)
.forEach(...);The iterate overload with a predicate is available since Java 9. On Java 8, use IntStream.iterate(1, i -> i + 2).limit(5) to get 1, 3, 5, 7, 9.
Compatibility and Version Notes
IntStream.rangeClosed has been available since Java 8 and remains part of the standard Stream API in later versions. It requires no external dependencies. Keep in mind that the iterate overload with a predicate was introduced in Java 9, so code using it will not compile on Java 8.
When using rangeClosed in a multi-threaded context, you can call .parallel() on the stream. A numeric range source can be split across threads, but any stream operations must be independent and stateless for correct results.
For most use cases, IntStream.rangeClosed is a clean, expressive way to generate an inclusive range of integers. It integrates with the rest of the Stream API and avoids manual index management. Just remember the inclusive endpoint, the fixed step of 1, and the empty range behavior when the start is greater than the end.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.