栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java Streams-标准偏差

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java Streams-标准偏差

您可以将自定义收集器用于此任务,以计算平方和。内置

DoubleSummaryStatistics
收集器不跟踪它。专家组在此主题中对此进行了讨论,但最终未实现。计算平方和的困难在于对中间结果求平方时可能发生溢出。

static class DoubleStatistics extends DoubleSummaryStatistics {    private double sumOfSquare = 0.0d;    private double sumOfSquareCompensation; // Low order bits of sum    private double simpleSumOfSquare; // Used to compute right sum for non-finite inputs    @Override    public void accept(double value) {        super.accept(value);        double squarevalue = value * value;        simpleSumOfSquare += squarevalue;        sumOfSquareWithCompensation(squarevalue);    }    public DoubleStatistics combine(DoubleStatistics other) {        super.combine(other);        simpleSumOfSquare += other.simpleSumOfSquare;        sumOfSquareWithCompensation(other.sumOfSquare);        sumOfSquareWithCompensation(other.sumOfSquareCompensation);        return this;    }    private void sumOfSquareWithCompensation(double value) {        double tmp = value - sumOfSquareCompensation;        double velvel = sumOfSquare + tmp; // Little wolf of rounding error        sumOfSquareCompensation = (velvel - sumOfSquare) - tmp;        sumOfSquare = velvel;    }    public double getSumOfSquare() {        double tmp =  sumOfSquare + sumOfSquareCompensation;        if (Double.isNaN(tmp) && Double.isInfinite(simpleSumOfSquare)) { return simpleSumOfSquare;        }        return tmp;    }    public final double getStandardDeviation() {        return getCount() > 0 ? Math.sqrt((getSumOfSquare() / getCount()) - Math.pow(getAverage(), 2)) : 0.0d;    }}

然后,您可以将此类用于

Map<String, Double> standardDeviationMap =    list.stream()        .collect(Collectors.groupingBy( e -> e.getCar(), Collectors.mapping(     e -> e.getHigh() - e.getLow(),     Collector.of(         DoubleStatistics::new,         DoubleStatistics::accept,         DoubleStatistics::combine,         d -> d.getStandardDeviation()     ) )        ));

这会将输入列表收集到一个映射中,该映射中的值对应于

high - low
同一键的标准偏差。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/609940.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号