使用 Java 8 流计算加权平均值

编程入门 行业动态 更新时间:2024-10-25 04:25:34
本文介绍了使用 Java 8 流计算加权平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何计算 Map 的加权平均值,其中 Integer 值是要平均的 Double 值的权重.例如:地图有以下元素:

How do I go about calculating weighted mean of a Map<Double, Integer> where the Integer value is the weight for the Double value to be averaged. eg: Map has following elements:

  • (0.7, 100)//值为 0.7,权重为 100
  • (0.5, 200)
  • (0.3, 300)
  • (0.0, 400)
  • 我希望使用 Java 8 流应用以下公式,但不确定如何同时计算分子和分母并同时保留它.这里如何使用reduction?

    I am looking to apply the following formula using Java 8 streams, but unsure how to calculate the numerator and denominator together and preserve it at the same time. How to use reduction here?

    推荐答案

    您可以为此任务创建自己的收集器:

    You can create your own collector for this task:

    static <T> Collector<T,?,Double> averagingWeighted(ToDoubleFunction<T> valueFunction, ToIntFunction<T> weightFunction) { class Box { double num = 0; long denom = 0; } return Collector.of( Box::new, (b, e) -> { b.num += valueFunction.applyAsDouble(e) * weightFunction.applyAsInt(e); b.denom += weightFunction.applyAsInt(e); }, (b1, b2) -> { b1.num += b2.num; b1.denom += b2.denom; return b1; }, b -> b.num / b.denom ); }

    此自定义收集器采用两个函数作为参数:一个是返回值以用于给定流元素的函数(作为 ToDoubleFunction),另一个返回权重(作为 ToIntFunction).它使用一个辅助本地类在收集过程中存储分子和分母.每次接受一个条目时,分子会随着该值与其权重相乘的结果而增加,而分母会随着权重而增加.然后完成器将两者的除法返回为 Double.

    This custom collector takes two functions as parameter: one is a function returning the value to use for a given stream element (as a ToDoubleFunction), and the other returns the weight (as a ToIntFunction). It uses a helper local class storing the numerator and denominator during the collecting process. Each time an entry is accepted, the numerator is increased with the result of multiplying the value with its weight, and the denominator is increased with the weight. The finisher then returns the division of the two as a Double.

    示例用法如下:

    Map<Double,Integer> map = new HashMap<>(); map.put(0.7, 100); map.put(0.5, 200); double weightedAverage = map.entrySet().stream().collect(averagingWeighted(Map.Entry::getKey, Map.Entry::getValue));

    更多推荐

    使用 Java 8 流计算加权平均值

    本文发布于:2023-11-22 13:10:59,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1617544.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:平均值   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!