Java Lambda Stream group By和求和整数值/平均值

我有一个客户对象列表(客户:int id,bool isActive,int billingCount,...)并且想要 billingCount 的总和和平均值。不幸的是,我的代码不起作用。我需要如何更改代码才能正常工作?


sum 和 average sould 看起来像这样:


真1234


假 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)


撒科打诨
浏览 620回答 3
3回答

凤凰求蛊

通过您的map电话,您正在将您转换Stream<Customer>为Stream<Boolean>关于您的活跃和非活跃客户的正确和错误的流。您不能getBillingCount在布尔值上调用 Customer's。您可以使用收集partitioningBy器按布尔值进行分组,而无需事先map调用。下游收集器可以是同时收集总和和平均值的summarizingInt收集器(加上一些您可能不需要的其他收集器:计数、最大值、最小值)。Map<Boolean,&nbsp;Integer>&nbsp;stats&nbsp;=&nbsp;customer.stream() &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.partitioningBy(Customer::getIsActive, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Collectors.summarizingInt(Customer::getBillingCount)));这应该让您在一个声明中true获得统计数据。false

ABOUTYOU

您不需要使用map. 请参见下面的示例:List<Customer> customers = Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; new Customer(10, true, 5),&nbsp; &nbsp; &nbsp; &nbsp; new Customer(11, true, 3),&nbsp; &nbsp; &nbsp; &nbsp; new Customer(20, false, 12),&nbsp; &nbsp; &nbsp; &nbsp; new Customer(21, false, 11));Map<Boolean, Integer> sum = customers&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Customer::isActive, Collectors.summingInt(Customer::getBillingCount)));System.out.println(sum);Map<Boolean, Double> avg = customers&nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.groupingBy(Customer::isActive, Collectors.averagingInt(Customer::getBillingCount)));System.out.println(avg);上面的代码打印:{false=23, true=8}{false=11.5, true=4.0}

婷婷同学_

你真的需要一张活跃和不活跃的地图吗?简单地说:IntSummaryStatistics summaryActive = customer.stream()&nbsp; &nbsp; &nbsp; &nbsp; .filter(Customer::getIsActive)&nbsp; &nbsp; &nbsp; &nbsp; .mapToInt(Customer::getBillingCount)&nbsp; &nbsp; &nbsp; &nbsp; .summaryStatistics();long sumActive&nbsp; = summary.getSum();double averageActive = summary.getAverage();您可以通过将过滤器替换为非活动来执行相同的操作.filter(c -> !c.getIsActive())
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java