Home » Java stream reduce sum

Java stream reduce sum

  • by
Sum integers with Java stream

1. Overview

In this article, we will learn to implement reduce sum with Java stream.

2. Java stream reduce sum

You can sum a list of integers with Java streams using any of the following options:

2.1. Sum by reducing the stream

A reduction operation takes a sequence of input elements and combines them into a single summary result by repeatedly summing the values.

Reduction operates on the whole stream rather than on individual elements. You can use both the reduce and sum methods available in streams classes together to calculate the sum.

For example, the following code calculates the sum of a stream of numbers using reduce method:

// list
int sum = marks.stream().reduce(0, (x,y) -> x+y);
OR
int sum = marks.stream().reduce(0, Integer::sum);
// map
integers.values().stream().mapToInt(i -> i).reduce(0, (x,y) -> x+y);
OR
integers.values().stream().reduce(0, Integer::sum);

These reduction operations can run safely in parallel with no modification.

int sum = numbers.parallelStream().reduce(0, Integer::sum);

2.2. Use sum method

A stream can contain elements of any type whereas an IntStream represents a sequence of primitive int-valued elements. We can convert a stream of arbitrary type to an IntStream by using the mapToInt method on the stream.

  • IntStream mapToInt(ToIntFunction<? super T> mapper)

The mapToInt method maps any element to an int. A IntStream has a sum method to sum all the integer elements in the stream. 

List<String> marks = Arrays.asList("98", "88", "67", "56", "90");
int sum = marks.stream().mapToInt(Integer::intValue).sum();
OR
int sum = marks.stream().mapToInt(i -> i.intValue()).sum();
System.out.println(sum);

You can use the mapToLong and mapToDouble methods to map an element to a long and a double, respectively. It will return a DoubleStream and a LongStream accordingly. Then, you can invoke the sum to return a double or long.

List<String> marks = Arrays.asList("98", "88", "67", "56", "90");
long sumAsLong = marks.stream()
        .mapToLong(Integer::intValue)
        .sum();
double sumAsDouble = marks.stream()
        .mapToDouble(Integer::intValue)
        .sum();

2.3. Use Collectors.summingInt

The Collectors.summingInt is a Collector that sums the primitive integers. It accepts a ToIntFunction (Integer::intValue) to convert an element in the stream to an int.

// list
fruits.stream().collect(Collectors.summingInt(Integer::intValue));
// for map
integers.values().stream().collect(Collectors.summingInt(Integer::intValue));

2.4. Use Collectors.summarizingInt

The Collectors.summarizingInt is a collector that can calculate the sum of the primitive integers.

int sum = integers.stream().collect(Collectors.summarizingInt(Integer::intValue)).getSum();

2.5. Use LongAdder

Java 8 introduces a very effective LongAdder accumulator designed to speed up summarizing in parallel streams and multi-thread environments. 

LongAdder a = new LongAdder();
map.values().parallelStream().forEach(a::add);
sum = a.intValue();

3. Conclusion

To sum up, you can calculate the Sum integers with the Java stream. To learn more about the Java concepts, refer to our articles.

Leave a Reply

Your email address will not be published.