0

This is my Employee class:

@AllArgsConstructor
@Getter
public static class Employee {
    public enum Gender {
        MALE, FEMALE
    }

    private long id;
    private String name;
    private Gender gender;
    private LocalDate dob;
    private double income;

    public static List<Employee> persons() {
        Employee p1 = new Employee(1, "Jake", Gender.MALE, LocalDate.of(1971,
            Month.JANUARY, 1), 2343.0);
        Employee p2 = new Employee(2, "Jack", Gender.MALE, LocalDate.of(1972,
            Month.JULY, 21), 7100.0);
        Employee p3 = new Employee(3, "Jane", Gender.FEMALE, LocalDate.of(1973,
            Month.MAY, 29), 5455.0);
        Employee p4 = new Employee(4, "Jode", Gender.MALE, LocalDate.of(1974,
            Month.OCTOBER, 16), 1800.0);
        Employee p5 = new Employee(5, "Jeny", Gender.FEMALE, LocalDate.of(1975,
            Month.DECEMBER, 13), 1234.0);
        Employee p6 = new Employee(6, "Jason", Gender.MALE, LocalDate.of(1976,
            Month.JUNE, 9), 3211.0);
    
        List<Employee> persons = Arrays.asList(p1, p2, p3, p4, p5, p6);
    
        return persons;
    }
}
    

I want to calculate the total income per gender.

My code:

Map<Gender, Double> totalIncomeByGender = Employee.persons().stream()
    .collect(Collectors.groupingBy(
        Employee::getGender, 
        Collectors.reducing(0,Employee::getIncome,Double::sum)));

I'm getting a compilation error:

java: no suitable method found for reducing(int,Employee::getIncome,Double::sum)
    method java.util.stream.Collectors.<T>reducing(T,java.util.function.BinaryOperator<T>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))
    method java.util.stream.Collectors.<T>reducing(java.util.function.BinaryOperator<T>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))
    method java.util.stream.Collectors.<T,U>reducing(U,java.util.function.Function<? super T,? extends U>,java.util.function.BinaryOperator<U>) is not applicable
      (inference variable U has incompatible bounds
        lower bounds: java.lang.Double,java.lang.Object
        lower bounds: java.lang.Double,java.lang.Integer)

Can somebody please explain the reason of this error, and what is a correct way to write this code?

I've written a similar stream in a different code, and it worked. How can I fix it?

3
  • Usually the error says why there's an error. Commented Sep 11, 2022 at 7:24
  • Use 0D as identity(D means it's double). When using 0, you provide an int. And you should always show the exact error, when asking about errors you get... Commented Sep 11, 2022 at 7:33
  • Aside from that, did you omit the constructor of Employee? Commented Sep 11, 2022 at 7:37

1 Answer 1

1

You've provided an identity of an incorrect type - Integer, because numeric literal 0 is of type int.

Meanwhile, the result of reduction should be of type Double. That results in the type mismatch in the classifier function, since it gives a Double as required according to the resulting type of the map.

To fix the compilation error, you need to change the identity to 0d or 0D.

Map<Employee.Gender,Double> totalIncomeByGender = Employee.persons().stream()
    .collect(Collectors.groupingBy(
        Employee::getGender,
        Collectors.reducing(0D, Employee::getIncome,Double::sum)
    ));
Sign up to request clarification or add additional context in comments.

1 Comment

Or use Collectors.toMap(Employee::getGender, Employee::getIncome, Double::sum)… If the groups can be really large, it’s better to use Collectors.groupingBy(Employee::getGender, Collectors.summingDouble(Employee::getIncome)) though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.