How to calculate Employees Salaries Java 8 summingInt

By | 2017-04-22T10:40:07+00:00 April 22nd, 2017|java8|1 Comment

In this tutorials, I am going to show you how to calculate the total salaries of employees using Java 8 summingInt.

Java 8 summingInt :

summingInt(ToIntFunction<? super T> mapper) is method in Collector class. It produces the sum of a 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.

Employee.java


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);
    }

}

[box type=”success” align=”alignleft” class=”” width=”100%”]

Total Employees Salary : 43500

[/box]

Calculate the Sum by Dept :

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);
    }
    
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

Total Employees Salary : 43500
Total Employees Salary of 1000 Dept : 14000

[/box]

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

One Comment

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

    Thank you very much for your blog.

    I enjoyed reading this article.

Leave A Comment