Introduction to Range in Java
In Java, the Range method is available in IntStream as well as LongStream class. In IntStream class, it helps in returning sequentially ordered values of IntStream in the range mentioned as parameters of the function. In this method, startInclusive (inclusive) and endExclusive (exclusive) are the two parameters that are used with an incremental step as, it is mentioned, the starting value will be included and the ending value will be excluded. In the case of LongStream, the only difference is that LongStream values are getting added.
Syntax of Range in Java
Let us see the syntax of the range method in Java.
1. Syntax of IntStream range
static IntStream range(int startInclusive, int endExclusive)
Parameters:
- IntStream: This is a sequence of int-valued elements which are of primitive type.
- startInclusive: The initial value which is included in the range.
- endExclusive: The last value or upper bound which is excluded in the range.
Return Value:
This method returns a sequential IntStream of int elements mentioned in the range as parameters.
2. Syntax of LongStream range
static LongStream range(int startInclusive, int endExclusive)
Parameters:
- LongStream: This is a sequence of long-valued elements which are of primitive type.
- startInclusive: The initial value which is included in the range.
- endExclusive: The last value or upper bound which is excluded in the range.
Return Value:
This method returns a sequential LongStream of long elements mentioned in the range as parameters.
How does Range Function work in Java?
First, let us see how the IntStream range works in Java. Similar to other classes in Java, this class also needs a package that has to be imported first. That is, for working with the IntStream class, import the package import java.util.stream.IntStream. Once it is imported, create an IntStream so that elements can be added to it. After creating the stream, use the method range( ) for adding the elements. On executing the code, a sequential ordered IntStream will be returned by an incremental step of one within the mentioned range in the parameter.
For printing each element, use the method forEach as shown below.

4.8 (13,332 ratings)
View Course
intStream.forEach(System.out::println);
In the case of LongStream, first import the package java.util.stream.LongStream. Similar to IntStream functioning, once the package is imported, create a LongStream so that elements can be added to it. After creating the stream, use the method range( ) for adding the elements. On executing the code, a sequential ordered LongStream will be returned by an incremental step of one within the mentioned range in the parameter.
For printing each and every element using the method forEach as shown below.
LongStream.forEach(System.out::println);
An equivalent way of printing sequence of increasing elements can be generated sequentially with the help of a for loop as shown below.
for (inti = startInclusive; i<endExclusive ; i++)
{... . . . }
Examples of Range in Java
Given below are the examples mentioned:
Example #1
Java program to implement IntStream range function.
Code:
// IntStream range implementation using Java
import java.util.*;
//import the package for IntStream
import java.util.stream.IntStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create an IntStream
IntStream st = IntStream.range(32, 45);
// Display the elements in the range mentioned as 32 and 45 where 32 is included and 45 is excluded
System.out.println("The elements are:");
st.forEach(System.out::println);
} }
Output:
First, import the package java.util.stream.IntStream. Then, create an IntStream st for adding elements to it. During the creation of the stream, use the method range (32, 45) for adding the elements where 32 is included and 45 is excluded. On executing the code, a sequential ordered IntStream will be returned from 32 to 44 by an incremental step of one as shown in the sample output.
Example #2
Java program to implement LongStream range function.
Code:
// LongStream range implementation using Java
import java.util.*;
//import the package for LongStream
import java.util.stream.LongStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create a LongStream
LongStream st = LongStream.range(1000001L, 1000010L);
// Display the elements in the range mentioned as 1000001L and 1000010L where 1000001L is included and 1000010L is excluded
System.out.println("The elements are:");
st.forEach(System.out::println);
} }
Output:
Similar to the above program, import the package java.util.stream.LongStream. Then, create a LongStreamst with a method range (100001L, 100010L) for adding elements to it. On executing the code, a sequential ordered LongStream will be returned from 100001L to 100010L by an incremental step of one as shown in the sample output.
Example #3
Java program to implement LongStream and IntStream range function in combination.
Code:
import java.util.*;
//import the package for IntStream
import java.util.stream.IntStream;
//import the package for LongStream
import java.util.stream.LongStream;
public class RangeExample {
// main method
public static void main(String[] args)
{
// Create an IntStream
IntStream str = IntStream.range(32, 45);
// Display the elements in the range mentioned as 32 and 45 where 32 is included and 45 is excluded
System.out.println("The IntStream elements are:");
str.forEach(System.out::println);
// Create a LongStream
LongStream st = LongStream.range(1000001L, 1000010L);
// Display the elements in the range mentioned as 1000001L and 1000010L where 1000001L is included and 1000010L is excluded
System.out.println("The LongStream elements are:");
st.forEach(System.out::println);
} }
Output:
Import the package java.util.stream.IntStream and java.util.stream.LongStream. Then, create IntStreamstr and LongStreamst for adding elements to it. During creating the stream, use the method range (32, 45) for adding the elements in IntStream where 32 is included and 45 is excluded. Similarly, use method range (100001L, 100010L) for adding elements in LongStream. On executing the code, a sequential ordered IntStream will be returned from 32 to 44 and LongStream will be returned from 100001L to 100010L by an incremental step of 1.
Conclusion
The range method in Java is used for returning sequentially ordered values of IntStream and LongStream in the range mentioned as parameters of the function. In this article, several aspects of the same are discussed in detail.
Recommended Articles
This is a guide to Range in Java. Here we discuss the introduction to Range in Java, how does the function work along with programming examples. You may also have a look at the following articles to learn more –