0Pricing
Java Academy · Lesson

Range and Aggregate Operations

range, sum, average, summaryStatistics.

Generating Ranges

IntStream.range(start, end) produces consecutive integers from start up to but not including end.

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        IntStream.range(0, 5).forEach(System.out::println);
    }
}

rangeClosed Includes the End

IntStream.rangeClosed(start, end) includes the final value, so rangeClosed(1, 5) yields 1 through 5.

import java.util.stream.IntStream;

public class Main {
    public static void main(String[] args) {
        int sum = IntStream.rangeClosed(1, 5).sum();
        System.out.println(sum);
    }
}

All lessons in this course

  1. IntStream and LongStream
  2. Range and Aggregate Operations
  3. Mapping to and from Object Streams
  4. Generating Primitive Streams
← Back to Java Academy