繁体   English   中英

我们如何在java8中的DoubleSummaryStatistics对象中自定义count,avg,sum,min和max的顺序

[英]How can we customize order of count,avg,sum,min and max in DoubleSummaryStatistics object in java8

我按以下格式获得低于输出,这是我认为的默认值。

{"count":100,"sum":25640.13,"min":2.65,"max":483.91,"average":256.4013}

但我想改变这种格式如下。

{"sum":"25640.13","avg":"256.40","max":"483.91","min":"2.65","count":100}

下面我在java类中使用的代码。

 @Override
public DoubleSummaryStatistics getStatistic() {
    logger.info("Getting statistic");
    Set<Entry<Long, Transaction>> endtrySet = statisticHistory.entrySet();
    List<Double> amountList = new ArrayList<>();

    for (Entry<Long, Transaction> entry : endtrySet) {
        if (((entry.getValue().getDate())).isAfter(Instant.now().minusSeconds(windowInMs)))
            amountList.add((entry.getValue().getAmount()).doubleValue());
    }
    return amountList.stream().mapToDouble((x) -> x).summaryStatistics();

}

如何重新排列json格式?

为了更好的理解,用简单的语法粘贴上面的方法。 示例代码方法..

    public DoubleSummaryStatistics getStatisdtic() {
    logger.info("Getting statistic");
    Set<BigDecimal> endtrySet = null ; //= getting this from a other resource
    List<Double> amountList = new ArrayList<>();

    for (BigDecimal entry : endtrySet) {
            amountList.add((entry).doubleValue());
    }
    return amountList.stream().mapToDouble((x) -> x).summaryStatistics();

}

由于您无法编辑DoubleSummaryStatistics (您无法在其中添加自己的json特定注释),您可以做的是创建自己的类MyDoubleSummaryStatistics ,它将根据您想要的任何结果创建。

但是如果你在DoubleSummaryStatistics上调用toString ,为什么不分别调用每个字段,比如DoubleSummaryStatistics ::getAverage等,并构建你需要/想要的任何字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM