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

Java 8 code example showing range() and rangeClosed() usage
package com.javabrahman.java8.streams;
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 + " "));
    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 + " "));
    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-2022, all rights reserved.