我有一个“客户对象列表”列表(客户:int id,bool isActive,int billingCount等),并且想要billingCount的总和和平均值。 不幸的是,我的代码无法正常工作。 我该如何更改代码才能正常工作?
总和和平均灵魂看起来像这样:
真实1234
错误1234
Map sum = customer.stream()
.map(c -> c.getIsActive())
.collect(Collectors.groupingBy(c -> c, Collectors.summingInt(Customer::getBillingCount)));
Map 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>)
method java.util.stream.Stream.collect(java.util.function.Supplier,java.util.function.BiConsumer,java.util.function.BiConsumer) is not applicable
(cannot infer type-variable(s) R
(actual and formal argument lists differ in length))
method java.util.stream.Stream.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)