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.
- 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.
- 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.
- 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
You may also like-
No comments:
Post a Comment