Introduction
In Java, the Range method is available in both the IntStream
and LongStream
classes. In the IntStream class it helps to return the sequential value of the IntStream in the range of function parameters. In this method, startInclusive(inclusive)
and endExclusive(exclusive)
are the two parameters used with the incremental step which, as mentioned before, will include the start value and exclude the end value. In the case of LongStream, the only difference is the addition of the LongStream value.
Range syntax
Let’s look at the syntax of the range method in Java.
Syntax of the IntStream range
|
|
Parameters.
- IntStream: this is a sequence of int-valued elements of primitive type.
- startInclusive: the initial value to include in the range.
- endExclusive: the last value or upper limit to be excluded from the range.
Return Value.
This method returns as an argument a continuous int stream of the int elements mentioned in the range.
Syntax of the LongStream range
|
|
Parameters.
- LongStream: this is a sequence of long-valued elements of primitive type.
- startInclusive: the initial value to be included in the range.
- endExclusive: the last value or upper limit to be excluded from the range.
Return Value.
The method returns a continuous long stream of the long elements mentioned in the range as arguments.
How do Range functions work in Java?
First, let’s look at how the IntStream range works in Java. Similarly to other classes in Java, this class requires a package that must first be imported. That is, in order to use the IntStream class, import the package java.util.stream.IntStream
. Once imported, an IntStream is created so that elements can be added to it. After the stream is created, elements are added using the method range()
. When the code is executed, a sequential ordered IntStream will be returned by an incremental step within the range mentioned in the argument.
To print each element, use the method shown below.
|
|
For LongStream, first import the package ``java.util.stream. Similar to the function of IntStream, once the package is imported, a LongStream is created so that elements can be added to it. Once the stream is created, elements are added using the method range(). When the code is executed, a sequentially ordered LongStream is returned by an incremental step within the range mentioned in the parameters.
For printing each element using the method shown below.
|
|
With the help of a for loop, an equivalent print sequence of incrementing elements can be generated in order, as shown below.
Range example in Java
The following examples are mentioned.
Example #1
Java program implementing the IntStream Range function.
Code.
|
|
Output.
First, import the package java.util.stream.IntStream
. Then, an IntStream st
is created and used to add elements to it. In the process of creating the stream, elements are added using the method range (32, 45), which includes 32 elements and excludes 45 elements. When the code is executed, an ordered IntStream will be returned in an incremental step from 32 to 44, as shown in the example output.
Example #2
Java program implementing the LongStream range function.
Code.
|
|
Output.
Similar to the above program, import the package java.util.stream. Then, create a LongStreamst with method
range (100001L, 100010L)
for adding elements to it. When the code is executed, it will return from 100001L to 100010L by an incremental step, as shown in the sample output.
Example #3
Java program for combined implementation of LongStream and IntStream range functions.
Code.
|
|
Output.
Import the packages java.util.stream.IntStream
and java.util.stream.LongStream
. Then, create IntStreamstr and LongStreamst to add elements to them. During the creation of the stream, elements are added to the IntStream using the method range(32, 45)
, which includes 32 and excludes 45. similarly, elements are added to the LongStream using the method range(100001L, 100010L)
. When the code is executed, the sequence ordered IntStream will return from 32 to 44 and the LongStream will return from 100001L to 100010L by incrementing step 1.
Conclusion
The range method in Java is used to return the sequential values of IntStream and LongStream over the range of function parameters.