This Java 8 code tip shows, with code examples, when and how to use static methods 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}
, where 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.
Are you struggling with Java homework? Programming can be a tricky subject, and if Java is giving you trouble, don't worry - help is at hand. Whether it's basic syntax or more advanced concepts like classes and objects, there are plenty of resources available to provide you with JavaScript homework help.
The internet is full of tutorials and guides that can help you understand Java programming. From written manuals to videos and interactive exercises, you can find the perfect way to learn Java for yourself. There are also resources like online forums that allow fellow Java learners to discuss their questions and problems with each other - or even ask experienced Java developers for advice.
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.
Difference between range() and rangeClosed() methods
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
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 + " ")); } }
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.
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
