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

1
static IntStream range(int startInclusive, int endExclusive)

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

1
static LongStream range(int startInclusive, int endExclusive)

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.

1
intStream.forEach(System.out::println);

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.

1
LongStream.forEach(System.out::println);

With the help of a for loop, an equivalent print sequence of incrementing elements can be generated in order, as shown below.

1
2
for (inti = startInclusive; i<endExclusive ; i++)
{... . . . }

Range example in Java

The following examples are mentioned.

Example #1

Java program implementing the IntStream Range function.

Code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// 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.

Introduction to the range function in Java

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// LongStream range implementation using Java
import java.util.*;
//import the package for LongStream
import java.util.stream;
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.

Introduction to the range function in Java

Similar to the above program, import the package java.util.stream. Then, create a LongStreamst with methodrange (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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
//import the package for IntStream
import java.util.stream;
//import the package for LongStream
import java.util.stream;
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.

Introduction to the range function in Java

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.