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

surendrapanday :

I am getting below output in following format, which is default I think.

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

But I want to change this format as below.

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

Below code I am using in java class.

 @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();

}

How can I rearrange the json format?

For more understanding, pasting above method with simple syntax. Sample Code Method..

    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();

}
Eugene :

Since you can't edit DoubleSummaryStatistics (you can't add your own json specific annotations inside it), the thing you can do is create your own class MyDoubleSummaryStatistics that would be created from whatever result you want.

But in case you are calling toString on that DoubleSummaryStatistics, why not call each field separately from it, like DoubleSummaryStatistics ::getAverage and the like and build whatever string you need/want.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=71333&siteId=1