IntStream range() method in Java


The range() method in the IntStream class in Java is used to return a sequential ordered IntStream from startInclusive to endExclusive by an incremental step of 1. This includes the startInclusive as well.

The syntax is as follows −

static IntStream range(int startInclusive, int endExclusive)

Here, the parameter startInclusive includes the starting value, whereas endExclusive excludes the last value

To work with the IntStream class in Java, import the following package −

import java.util.stream.IntStream;

Create an IntStream and add stream elements in a range using range() method. This returns a sequential ordered IntStream by an incremental step of 1 within the range −

intStream.forEach(System.out::println);

The following is an example to implement IntStream range() method in Java −

Example

 Live Demo

import java.util.*;
import java.util.stream.IntStream;

public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(20, 30);
      intStream.forEach(System.out::println);
   }
}

Output

20
21
22
23
24
25
26
27
28
29

Updated on: 30-Jul-2019

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements