Thursday, April 21, 2022

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

Collectors Class in Java Stream API is an implementation of Collector interface and provides many useful operations that can be used with Java Streams. One such group of methods, which can be classified as Collectors.summing methods, is used to add the stream elements. There are following summing methods for types int, long and double respectively.

  1. public static <T> Collector<T,?,Integer> summingInt(ToIntFunction<? super T> mapper)- This method is used to get the sum of stream of ints, if there is no element in the stream then the result is 0.
  2. public static <T> Collector<T,?,Long> summingLong(ToLongFunction<? super T> mapper)- This method is used to get the sum of stream of longs, if there is no element in the stream then the result is 0.
  3. public static <T> Collector<T,?,Double> summingDouble(ToDoubleFunction<? super T> mapper)- This method is used to get the sum of stream of doubles, if there is no element in the stream then the result is 0.

As you can notice the argument passed to these methods are of type ToIntFunction, ToLongFunction and ToDoubleFunction respectively. These are functional interfaces which are implementd to extract the property to be summed meaning their implementation produces a int-valued, long-valued and double-valued result.

Collectors.summingInt() Java example

In this example we’ll get the sum of the List elements where elements are of type Integer.

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

public class CollectorSumming {

  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(11, 5, 2, 10, 1);
      int sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));
      System.out.println("Sum of list elements: " + sum);

  }
}

Output

Sum of list elements: 29

Collectors.summingLong() Java example

In this example we’ll get the sum of the List elements where elements are of type Long.

public class CollectorSumming {

  public static void main(String[] args) {
    List<Long> numbers = Arrays.asList(11L, 5L, 2L, 10L, 1L);
      long sum = numbers.stream().collect(Collectors.summingLong(Long::longValue));
      System.out.println("Sum of list elements: " + sum);
  }
}

Output

Sum of list elements: 29

Collectors.summingDouble() Java example

In this example we’ll get the sum of the List elements where elements are of type Double.

public class CollectorSumming {

  public static void main(String[] args) {
    List<Double> numbers = Arrays.asList(11.5, 5.0, 2.65, 10.34, 7.89);
    double sum = numbers.stream().collect(Collectors.summingDouble(Double::doubleValue));
      System.out.println("Sum of list elements: " + sum);
  }
}

Output

Sum of list elements: 37.38

Collectors.summingInt with Custom Object Example

In this example we’ll use summing method to get the sum of salaries for all the employees.

Employee Class

public class Employee {
  private String empId;
  private int age;
  private String name;
  private char gender;
  private int salary;
  Employee(String empId, int age, String name, char gender, int salary){
    this.empId = empId;
    this.age = age;
    this.name = name;
    this.gender = gender;
    this.salary = salary;
  }
  public String getEmpId() {
    return empId;
  }

  public int getAge() {
    return age;
  }

  public String getName() {
    return name;
  }

  public char getGender() {
    return gender;
  }

  public int getSalary() {
    return salary;
  }
  @Override
  public String toString() {
      return "Emp Id: " +  getEmpId() + " Name: " + getName() + " Age: " + getAge();
  }
}
public class CollectorSumming {

  public static void main(String[] args) {
     List<Employee> empList = Arrays.asList(new Employee("E001", 40, "Ram", 'M', 5000), 
                  new Employee("E002", 35, "Shelly", 'F', 8000), 
                  new Employee("E003", 24, "Mark", 'M', 9000), 
                  new Employee("E004", 37, "Ritu", 'F', 11000),
                  new Employee("E005", 32, "Anuj", 'M', 6000), 
                  new Employee("E006", 28, "Amy", 'F', 14000));
    int sum = empList.stream().collect(Collectors.summingInt(Employee::getSalary));
      System.out.println("Sum of employee salaries: " + sum);
  }
}

Output

Sum of employee salaries: 53000

That's all for this topic Java Stream - Collectors.summingInt(), summingLong(), summingDouble(). If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Advanced Tutorial Page


Related Topics

  1. Java Stream - Collectors.averagingInt(), averagingLong(), averagingDouble()
  2. Java Stream - Collectors.teeing() With Examples
  3. Java Stream - Collectors.partitioningBy() With Examples
  4. Spliterator in Java
  5. Java Stream - limit() With Examples

You may also like-

  1. JVM Run-Time Data Areas - Java Memory Allocation
  2. Shallow Copy And Deep Copy in Java Object Cloning
  3. Is String Thread Safe in Java
  4. Java Program to Get Current Date and Time
  5. Angular First App - Hello world Example
  6. Java Application With User Input in Docker Container
  7. Spring MVC Pagination Example Using PagedListHolder
  8. Spring Boot Hello World Web Application Example

No comments:

Post a Comment