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

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

如何计算Map<Double, Integer>的加权平均值,其中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流应用以下公式,但是不确定如何同时计算分子和分母并保存.在这里如何使用归约?

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

    发布评论

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

    >www.elefans.com

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