У меня есть список объектов Customer (Список клиентов: int id, bool isActive, int billingCount, ...) и я хочу получить сумму и среднее значение billingCount.К сожалению, мой код не работал.Как мне нужно изменить код, чтобы он работал?
сумма и среднее значение должны выглядеть следующим образом:
true 1234
false 1234
Map<Boolean, Integer> sum = customer.stream()
.map(c -> c.getIsActive())
.collect(Collectors.groupingBy(c -> c, Collectors.summingInt(Customer::getBillingCount)));
Map<Boolean, Integer> average = customer.stream()
.map(c -> c.getIsActive())
.collect(Collectors.groupingBy(c -> c, Collectors.averagingInt(Customer::getBillingCount)));
}
Я получаю следующую ошибку:
Error:(146, 17) java: no suitable method found for collect(java.util.stream.Collector<Customer,capture#1 of ?,java.util.Map<java.lang.Object,java.lang.Integer>>)
method java.util.stream.Stream.<R>collect(java.util.function.Supplier<R>,java.util.function.BiConsumer<R,? super java.lang.Boolean>,java.util.function.BiConsumer<R,R>) is not applicable
(cannot infer type-variable(s) R
(actual and formal argument lists differ in length))
method java.util.stream.Stream.<R,A>collect(java.util.stream.Collector<? super java.lang.Boolean,A,R>) is not applicable
(inference variable T has incompatible bounds
lower bounds: java.lang.Object,Customer
lower bounds: java.lang.Boolean)