Saturday, October 11, 2025
HomeLanguagesJavaLongStream rangeClosed() in Java

LongStream rangeClosed() in Java

LongStream rangeClosed(long startInclusive, long endInclusive) returns an LongStream from startInclusive (inclusive) to endInclusive (inclusive) by an incremental step of 1.

Syntax :

static LongStream rangeClosed(long startInclusive, long endInclusive)

Parameters :

  1. LongStream : A sequence of primitive long-valued elements.
  2. startInclusive : The inclusive initial value.
  3. endInclusive : The inclusive upper bound.

Return Value : A sequential LongStream for the range of long elements.

Example :




// Implementation of LongStream rangeClosed
// (long startInclusive, long endInclusive)
import java.util.*;
import java.util.stream.LongStream;
  
class GFG {
  
    // Driver code
    public static void main(String[] args)
    {
        // Creating an LongStream
        LongStream stream = LongStream.rangeClosed(-4L, 3L);
  
        // Displaying the elements in range
        // including the lower and upper bound
        stream.forEach(System.out::println);
    }
}


Output:

-4
-3
-2
-1
0
1
2
3

Note : LongStream rangeClosed(long startInclusive, long endInclusive) basically works like a for loop. An equivalent sequence of increasing values can be produced sequentially as :

for (int i = startInclusive; i <= endInclusive ; i++) 
{
 ...
 ...
 ...
}
RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7103 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS