Java 8 - How to use range(), rangeClosed() methods of IntStream, LongStream with examples
This Java 8 code tip shows, with code examples, when and how to use static methods
In above definition
With the advent of Java 8, and Streams API, to generate a stream of a fixed sequence of numbers starting from an initial until a final value, where each number is an increment of 1 over the previous,
Difference between range() and rangeClosed() methods
Note - The input parameters of the
Next, let us see an example showing the two methods in action which is followed by an explanation of the code.
Example Java 8 code showing how to use range() and rangeClosed() methods
OUTPUT of the above code
Explanation of the code
range()
and rangeClosed()
available in java.util.stream.IntStream
and java.util.stream.LongStream
interfaces to create a stream of numbers starting from a specified initial value to an end value.
When to Use range() and rangeClosed() methods of IntStream, LongStream
Prior to Java 8, and Streams, generating fixed set of numbers in a sequence meant writing a for loop with a definition like this - for(int i=init; i<=n; i++){//logic goes here}
In above definition
init
is the starting value and n
is the last value till which sequential numbers are generated.With the advent of Java 8, and Streams API, to generate a stream of a fixed sequence of numbers starting from an initial until a final value, where each number is an increment of 1 over the previous,
LongStream
and IntStream
class's range()
and rangeClosed()
methods can be used.
How to use range() and rangeClosed() methods
The general format of static range()
and rangeClosed()
methods is similar for both IntStream
and LongStream
. Both the methods take a start value and an end value which are of type int
for IntStream
and long
for LongStream
. The output of the methods is a primitive stream with the type(int
/long
) of elements based on class on which the static methods are called.
range()
method generates a stream of numbers starting from start value but stops before reaching the end value, i.e start value is inclusive and end value is exclusive.
Example: IntStream.range(1,5)
generates a stream of ‘1,2,3,4
’ of type int
.rangeClosed()
method generates a stream of numbers starting from start value and stops after generating the end value, i.e start value is inclusive and end value is also inclusive.
Example: LongStream.rangeClosed(1,5)
generates a stream of ‘1,2,3,4,5
’ of type long
.Note - The input parameters of the
range()
& rangeClosed()
methods are named (startInclusive, endExclusive)
& (startInclusive, endInclusive)
to match their nature.Next, let us see an example showing the two methods in action which is followed by an explanation of the code.
Example Java 8 code showing how to use range() and rangeClosed() methods
Java 8 code example showing range() and rangeClosed() usage
package com.javabrahman.java8.streams;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public class RangeNRangeClosedExample {
public static void main(String args[]){
//IntStream.range() and IntStream.rangeClosed() examples
System.out.println("Using IntStream.range() & IntStream.rangeClosed()");
IntStream.range(1, 10).forEach(i -> System.out.print(i + " "));
System.out.println();
IntStream.rangeClosed(1, 10).forEach(i -> System.out.print(i + " "));
//LongStream.range() and LongStream.rangeClosed() examples
System.out.println("\n Using LongStream.range() & LongStream.rangeClosed()");
LongStream.range(1000000L, 1000005L).forEach(i -> System.out.print(i + " "));
System.out.println();
LongStream.rangeClosed(1000000L, 1000005L).forEach(i -> System.out.print(i + " "));
}
}
Using IntStream.range() & IntStream.rangeClosed() 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 Using LongStream.range() & LongStream.rangeClosed() 1000000 1000001 1000002 1000003 1000004 1000000 1000001 1000002 1000003 1000004 1000005
- The
main()
method ofRangeNRangeClosedExample
class, first starts with examples ofIntStream.range()
andIntStream.rangeClosed()
methods. - The stream elements generated are then printed using
Stream.forEach()
statement. While therange(1,9)
method generates numbers from1 to 9
,rangeClosed(1,10)
method generates numbers from1 to 10
. Elements of both the streams of typeint
are printed and the values printed are as expected. - Similarly,
LongStream.range()
andLongStream.rangeClosed()
methods are invoked next. Primitive streams of typelong
are created with values generated from1000000 to 1000004
(for range())
and1000000 to 1000005
(for rangeClosed())
as expected.
Java 8 Streams API tutorials on JavaBrahman
Streams API - Introduction & BasicsClick to Read tutorial on Streams API Basics Understanding Stream Operations | Intermediate and Terminal OperationsClick to Read Tutorial on Stream Operations Overview Mapping with Streams using map and flatMap methodsClick to Read how Mapping with Java8 Streams works Filtering and Slicing with Streams using filter,distinct,limit,skip methodsClick to Read how Filtering and Slicing with Java8 Streams works Matching with Streams using allMatch,anyMatch,noneMatch methodsClick to Read tutorial on matching with Streams API Stream API's findFirst,findAny methods' tutorialClick to Read tutorial on findFirst() and findAny() methods of Streams API 'Peeking' into a running Stream with Stream.peek() methodClick to Read tutorial on Stream.peek() method Creating Infinite Streams with iterate\generate methodsClick to Read tutorial on Creating Infinite Streams Reducing Streams using Streams.reduce methodClick to Read tutorial on Reducing Streams
Streams API - Introduction & BasicsClick to Read tutorial on Streams API Basics Understanding Stream Operations | Intermediate and Terminal OperationsClick to Read Tutorial on Stream Operations Overview Mapping with Streams using map and flatMap methodsClick to Read how Mapping with Java8 Streams works Filtering and Slicing with Streams using filter,distinct,limit,skip methodsClick to Read how Filtering and Slicing with Java8 Streams works Matching with Streams using allMatch,anyMatch,noneMatch methodsClick to Read tutorial on matching with Streams API Stream API's findFirst,findAny methods' tutorialClick to Read tutorial on findFirst() and findAny() methods of Streams API 'Peeking' into a running Stream with Stream.peek() methodClick to Read tutorial on Stream.peek() method Creating Infinite Streams with iterate\generate methodsClick to Read tutorial on Creating Infinite Streams Reducing Streams using Streams.reduce methodClick to Read tutorial on Reducing Streams