Skip to main content

Java Stream Collectors summingInt(), summingLong(), summingDouble()

In the tutorial Java Stream Collectors averagingInt(), averagingLong(), averagingDouble() we saw how Collectors class in Java Stream API provides methods like Collectors.averagingInt(), Collectors.averagingLong(), Collectors.averagingDouble() to get average of Stream on Integers, Long and Double respectively. Same way if you want to quickly add the stream elements there are Collectors.summing() methods which have the following forms for different types-

  • summingInt(ToIntFunction<? super T> mapper)- To get sum of stream of integers. If no elements are present, the result is 0.
  • summingLong(ToLongFunction<? super T> mapper)- To get sum of stream of longs. If no elements are present, the result is 0.
  • summingDouble(ToDoubleFunction<? super T> mapper)- To get sum of stream of doubles. If no elements are present, the result is 0.

Argument passed to the methods is of type ToIntFunction, ToLongFunction and ToDoubleFunction respectively. These are functional interfaces with methods applyAsInt(T value), applyAsLong(T value), applyAsDouble(T value) that produces a int-valued, long-valued and double-valued result.

Collectors.summingInt() example

In this example we’ll get the sum of List elements where List is storing integers.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SummingStreams {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(6, 7, 3, 8, 1);
    int sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));
    System.out.println("Sum of elements- " + sum);
  }
}
Output
Sum of elements- 25

Collectors.summingLong() example

In this example we’ll get the sum of List elements where List is storing longs.

public class SummingStreams {
  public static void main(String[] args) {
    List<Long> numbers = Arrays.asList(22L, 17L, 3L, 38L, 11L);
    long sum = numbers.stream().collect(Collectors.summingLong(Long::longValue));
    System.out.println("Sum of elements- " + sum);
  }
}
Output
Sum of elements- 91

Collectors.summingDouble() example

In this example we’ll get the sum of List elements where List is storing doubles.

public class SummingStreams {
  public static void main(String[] args) {
    List<Double> numbers = Arrays.asList(22.6, 17.4, 3.0, 57.7, 11.89);
    Double sum = numbers.stream().collect(Collectors.summingDouble(Double :: doubleValue));
    System.out.println("Sum of elements- " + sum);
  }
}
Output
Sum of elements- 112.59

Collectors.summing method with custom object

In this example we’ll use summing methods to get the sum of marks for each student in the List. Student class has a list of Marks and then we’ll have list of students. What is needed is to iterate Student list and then the mark list for each student and sum those marks.

Student class
public class Student {
  private int rollNo;
  private String name;
  private int age;
  private List marks;
  Student(int rollNo, String name, int age, List marks){
    this.rollNo = rollNo;
    this.name = name;
    this.age = age;
    this.marks = marks;
  }
  public int getRollNo() {
    return rollNo;
  }
  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  public List getMarks() {
    return marks;
  }

  @Override
  public String toString() {
    return "Roll Number: " +  getRollNo() 
        + " Name: " + getName() + " Age: " + getAge() + " Marks: " + getMarks();
  }
}
// Collectors.summingInt example
public class SummingStreams {
  public static void main(String[] args) {
    List<Student> studentList = Arrays.asList(new Student(1, "Mercy", 19, Arrays.asList(75, 80, 78)),
              new Student(2, "Ram", 20, Arrays.asList(99, 88, 91)),
              new Student(3, "Priscilla", 20, Arrays.asList(68, 78, 65)),
              new Student(4, "Jacques", 22, Arrays.asList(92, 89, 78)),
              new Student(5, "Peter", 19, Arrays.asList(68, 88, 75)));
    studentList.forEach(s -> {
       System.out.print("Name- " + s.getName());
       int marks = s.getMarks().stream().collect(Collectors.summingInt(Integer::intValue));
       System.out.println(" Marks " + marks);
      
    });
  }
}
Output
Name- Mercy Marks 233
Name- Ram Marks 278
Name- Priscilla Marks 211
Name- Jacques Marks 259
Name- Peter Marks 231

That's all for the topic Java Stream Collectors summingInt(), summingLong(), summingDouble(). If something is missing or you have something to share about the topic please write a comment.


You may also like