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 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.

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

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 + " "));
  }
}
 OUTPUT of the above code
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

Explanation of the code

  • The main() method of RangeNRangeClosedExample class, first starts with examples of IntStream.range() and IntStream.rangeClosed() methods.
  • The stream elements generated are then printed using Stream.forEach() statement. While the range(1,9) method generates numbers from 1 to 9, rangeClosed(1,10) method generates numbers from 1 to 10. Elements of both the streams of type int are printed and the values printed are as expected.
  • Similarly, LongStream.range() and LongStream.rangeClosed() methods are invoked next. Primitive streams of type long are created with values generated from 1000000 to 1000004 (for range()) and 1000000 to 1000005(for rangeClosed()) as expected.

 

Digiprove sealCopyright © 2014-2017 JavaBrahman.com, all rights reserved.