Java Streams - IntStream sum() example








IntStream sum() returns the sum of elements in this stream.

Syntax

sum has the following syntax.

int sum()

Example

The following example shows how to use sum.

import java.util.stream.IntStream;

public class Main {
  public static void main(String[] args) {
    IntStream i = IntStream.of(6,5,7,1, 2, 3, 3);
    int d = i.sum();
    System.out.println(d);
  }
}

The code above generates the following result.