Java 8 summingInt Example | Sum of Collection Elements | Java 8 Online Tutorialspoint

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show you how to calculate the total salaries of employees using Java 8 summingInt.
summingInt(ToIntFunction<? super T> mapper) is method in Collector class. It produces the sum of an integer-valued function applied to the input elements.
Like summingInt(), summingLong(ToLongFunction<? super T> mapper) also a method in same Collector class. It produces the sum of a long-valued function applied to the input elements.
Here is the example to calculate the sum of salaries of employees.
class Employee {
 private int id;
 private int deptId;
 private String name;
 private int salary;

 public Employee(int id, int deptId, String name, int salary) {
  super();
  this.id = id;
  this.deptId = deptId;
  this.name = name;
  this.salary = salary;
 }
 public int getId() {
  return id;
 }
 public String getName() {
  return name;
 }
 public int getSalary() {
  return salary;
 }
 public int getDeptId() {
  return deptId;
 }

}
Calculating the summingInt on Employees salary.
package com.onlinetutorialspoint.java8;

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

public class Java8_SummingInt {
 public static void main(String[] args) {
  List < Employee > employees = Arrays.asList(new Employee(1, 1000,
    "Chandra Shekhar", 6000),
   new Employee(1, 1000, "Rajesh", 8000), new Employee(1, 1004,
    "Rahul", 9000), new Employee(1, 1001, "Suresh", 12000),
   new Employee(1, 1004, "Satosh", 8500));

  int total = employees.stream().collect(
   Collectors.summingInt(Employee::getSalary));
  System.out.println("Total Employees Salary : " + total);
 }

}
Output:
Total Employees Salary : 43500
We can also calculate the sum of salaries by filtering the department ids by using Stream filters in Java 8.
package com.onlinetutorialspoint.java8;

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

public class Java8_SummingInt {
 public static void main(String[] args) {
  List < Employee > employees = Arrays.asList(new Employee(1, 1000, "Chandra Shekhar", 6000),
   new Employee(1, 1000, "Rajesh", 8000),
   new Employee(1, 1004, "Rahul", 9000),
   new Employee(1, 1001, "Suresh", 12000),
   new Employee(1, 1004, "Satosh", 8500)
  );

  int total = employees.stream()
   .collect(Collectors.summingInt(Employee::getSalary));

  int totalSalByDept = employees.stream().
  filter(name -> 1000 == name.getDeptId())
   .collect(Collectors.summingInt(Employee::getSalary));

  System.out.println("Total Employees Salary : " + total);
  System.out.println("Total Employees Salary of 1000 Dept : " + totalSalByDept);
 }

}
Output:
Total Employees Salary : 43500
Total Employees Salary of 1000 Dept : 14000
Happy Learning 🙂

Java 8 groupingBy Example
Parallel Streams in Java 8 Example
Java 8 how to remove duplicates from list
Resolve NullPointerException in Collectors.toMap
How to Filter null values from Java8 Stream
Java 8 How to get common elements from two lists
What is Association in Java
How to get Stream count in Java 8
Java8 Concatenate Arrays Example using Stream
AngularJs Search Filter Example
Hibernate Projection with Example
How to Merge Streams in Java 8
Spring Collection Dependency List Example
Java 8 foreach Example Tutorials
Java 8 Stream Filter Example with Objects

Java 8 groupingBy Example
Parallel Streams in Java 8 Example
Java 8 how to remove duplicates from list
Resolve NullPointerException in Collectors.toMap
How to Filter null values from Java8 Stream
Java 8 How to get common elements from two lists
What is Association in Java
How to get Stream count in Java 8
Java8 Concatenate Arrays Example using Stream
AngularJs Search Filter Example
Hibernate Projection with Example
How to Merge Streams in Java 8
Spring Collection Dependency List Example
Java 8 foreach Example Tutorials
Java 8 Stream Filter Example with Objects






Milana Travis
October 25, 2017 at 1:43 pm - Reply 

Thank you very much for your blog.
I enjoyed reading this article.










Milana Travis
October 25, 2017 at 1:43 pm - Reply 

Thank you very much for your blog.
I enjoyed reading this article.




Thank you very much for your blog.
I enjoyed reading this article.
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String