LongStream rangeClosed() method in Java


The rangeClosed() method of the LongStream class in Java returns a sequential ordered LongStream from startInclusive to endExclusive by an incremental step of 1. This is inclusive of the initial element and the last element.

The syntax is as follows −

static LongStream rangeClosed(long startInclusive, long endExclusive)

Here, startInclusive is the first value, whereas the endExclusive is the last.

To use the LongStream class in Java, import the following package −

import java.util.stream.LongStream;

The following is an example to implement LongStream rangeClosed() method in Java −

Example

 Live Demo

import java.util.stream.LongStream;

public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.rangeClosed(20L, 25L);
      longStream.forEach(System.out::println);
   }
}

Output

20
21
22
23
24
25

Updated on: 30-Jul-2019

204 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements