Updated June 14, 2023
Introduction to Range in Java
In Java, the Range method is accessible in the IntStream and LongStream classes. In the IntStream class, this method enables the return of sequentially ordered values within the specified range as function parameters. The two parameters used are startInclusive (inclusive) and endExclusive (exclusive), with an incremental step. It is important to note that the starting value is included while the ending value is excluded. The LongStream class follows a similar pattern, with the only distinction being the inclusion of LongStream values.
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 range includes the initial value.
- endExclusive: The last value or upper bound 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 range includes the initial value.
- endExclusive: The last value or upper bound 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. 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. Executing the code will result in a sequentially ordered IntStream being returned, with an incremental step of one, within the specified range provided in the parameter.
For printing each element, use the method forEach as shown below.
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. Executing the code will generate a sequential ordered LongStream within the specified range, with an incremental step of one.
For printing every element, use the method forEach, as shown below.
LongStream.forEach(System.out::println);
An equivalent way of printing a 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
Below are the examples:
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. To create a sequential ordered IntStream from 32 to 44 (inclusive of 32, exclusive of 45), use the range(32, 45) method during the stream creation. Executing the code will produce the desired result of a sequential ordered IntStream ranging from 32 to 44 with 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:
Like 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. Executing the code will generate a sequential ordered LongStream, which will span from 100001L to 100010L with an incremental step of one. The sample output will demonstrate this behavior.
Example #3
Java program to implement LongStream and IntStream range functions 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 to add elements to it. To create a stream in IntStream with elements ranging from 32 (inclusive) to 45 (exclusive), you can use the range(32, 45) method. Similarly, use method range (100001L, 100010L) for adding elements in LongStream. Executing the code will yield a sequentially ordered IntStream ranging from 32 to 44 (inclusive of 32, exclusive of 45). In addition, executing the code will also return a LongStream ranging from 100001L to 100010L with an incremental step of 1.
Conclusion
The range method in Java returns sequentially ordered IntStream and LongStream values in the range mentioned as function parameters. In this article, several aspects of the same are discussed in detail.
Recommended Articles
We hope that this EDUCBA information on “Range in Java” was beneficial to you. You can view EDUCBA’s recommended articles for more information.