I have requirement where I want sum values of all employee salaries in list
employeeList.foldLeft(java.math.BigDecimal.ZERO) { (accSal,emp) => accSal + getSalary(emp,designation,yearsOfExp) }
Here for each employee I want to call a function getSalary and sum the return values to get salaries of all employees
The above code does not seem to work for me , Keep getting error
Type mismatch expected:String actual:BigDecimal
Alternative to Mario's answer, if getSalary
returns a java.math.BigDecimal
, and the fold
should also return one, instead of a scala.math.BigDecimal
.
You can do this:
employeeList.foldLeft(java.math.BigDecimal.ZERO) {
(accSal, emp) =>
accSal.add(getSalary(emp,designation,yearsOfExp))
}
You can check the javadoc to confirm that they do not have a +
method, but an add
one.
And for that reason, it was calling the +
method that returns Strings.