Example of mapToDouble in Java 8

UPDATED: 08 October 2015

DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.

Source code (Employee.java)
public class Employee {
    private Long id;
    private String Firstname;
    private String Lastname;
    private Double Salary;

    public String getFirstname() {
        return Firstname;
    }

    public void setFirstname(String Firstname) {
        this.Firstname = Firstname;
    }

    public String getLastname() {
        return Lastname;
    }

    public void setLastname(String Lastname) {
        this.Lastname = Lastname;
    }

    public Double getSalary() {
        return Salary;
    }

    public void setSalary(Double Salary) {
        this.Salary = Salary;
    }
}

Source code
import java.util.ArrayList;
import java.util.List;

public class MapToDoubleExample {
    public static void main(String[] args) {
        /* Create an object of `Employee` and set values */
        Employee employeeChirag = new Employee();
        employeeChirag.setFirstname("Chirag");
        employeeChirag.setLastname("Thakor");
        employeeChirag.setSalary(93142.40D);
        
        Employee employeeVicky = new Employee();
        employeeVicky.setFirstname("Vicky");
        employeeVicky.setLastname("Thakor");
        employeeVicky.setSalary(53584.50D);        
        
        /**
         * - Create `List` of `Employee`
         * - Add `employeeChirag` and `employeeVicky` to List.
         */
        List<Employee> listItems = new ArrayList<>();
        listItems.add(employeeChirag);
        listItems.add(employeeVicky);
        
        /**
         * JavaDoc: `mapToDouble` method
         * Returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.
         * 
         * Explanation: `.mapToDouble(Employee::getSalary)` / `.mapToDouble(employee -> employee.getSalary())`
         * In this operation we are calling method `getSalary()` on each object of `Employee` in List.
         * That returns the `DoubleStream` of value(Salary) of all object in List.
         * And we are doing summation of all salary
         */
        Double totalSalaryPaid = listItems.stream()
                                          .mapToDouble(Employee::getSalary)
                                          .sum();
        
        /* Print the total salary paid */
        System.out.println("Total Salary Paid: " + totalSalaryPaid);
    }
}

Explanation: `.mapToDouble(Employee::getSalary)` / `.mapToDouble(employee -> employee.getSalary())
In this operation we are calling method getSalary() on each object of Employee in List. That returns the DoubleStream of value(Salary) of all object in List. And we are doing summation of all salary


Output
Total Salary Paid: 146726.9
Other References:
Example of mapToInt in Java 8
Example of mapToLong in Java 8


0 comments :