BigDecimal摘要统计信息 [英] BigDecimal summary statistics

查看:149
本文介绍了BigDecimal摘要统计信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个BigDecimals列表。

I have a list of BigDecimals.

List<BigDecimal> amounts = new ArrayList<>()

如何使用以上列表获取上述列表的摘要统计信息Java 8流不会丢失BigDecimal最多3-4个小数位的精度吗?

How do i get the summary statistics of the above list using Java 8 streams without losing precision of upto 3-4 decimal places of the BigDecimal?

推荐答案

我创建了一个 BigDecimal 此答案的通用摘要统计信息收集器的专业化,允许将其扩展到支持求和,因此也计算平均值:

I created a BigDecimal specialization of the generic summary statistics collector of this answer, which allowed extending it to also support summing, hence also calculating an average:

/**
 * Like {@code DoubleSummaryStatistics}, {@code IntSummaryStatistics}, and
 * {@code LongSummaryStatistics}, but for {@link BigDecimal}.
 */
public class BigDecimalSummaryStatistics implements Consumer<BigDecimal> {

    public static Collector<BigDecimal,?,BigDecimalSummaryStatistics> statistics() {
        return Collector.of(BigDecimalSummaryStatistics::new,
            BigDecimalSummaryStatistics::accept, BigDecimalSummaryStatistics::merge);
    }
    private BigDecimal sum = BigDecimal.ZERO, min, max;
    private long count;

    public void accept(BigDecimal t) {
        if(count == 0) {
            Objects.requireNonNull(t);
            count = 1;
            sum = t;
            min = t;
            max = t;
        }
        else {
            sum = sum.add(t);
            if(min.compareTo(t) > 0) min = t;
            if(max.compareTo(t) < 0) max = t;
            count++;
        }
    }
    public BigDecimalSummaryStatistics merge(BigDecimalSummaryStatistics s) {
        if(s.count > 0) {
            if(count == 0) {
                count = s.count;
                sum = s.sum;
                min = s.min;
                max = s.max;
            }
            else {
                sum = sum.add(s.sum);
                if(min.compareTo(s.min) > 0) min = s.min;
                if(max.compareTo(s.max) < 0) max = s.max;
                count += s.count;
            }
        }
        return this;
    }

    public long getCount() {
        return count;
    }

    public BigDecimal getSum()
    {
      return sum;
    }

    public BigDecimal getAverage(MathContext mc)
    {
      return count < 2? sum: sum.divide(BigDecimal.valueOf(count), mc);
    }

    public BigDecimal getMin() {
        return min;
    }

    public BigDecimal getMax() {
        return max;
    }

    @Override
    public String toString() {
        return count == 0? "empty": (count+" elements between "+min+" and "+max+", sum="+sum);
    }
}

它可以类似于<$ c $使用c> DoubleSummaryStatistics 对应物,如

It can be used similar to the DoubleSummaryStatistics counterpart, like

BigDecimalSummaryStatistics bds = list.stream().collect(BigDecimalSummaryStatistics.statistics());

作为一个完整的例子:

List<BigDecimal> list = Arrays.asList(BigDecimal.ZERO, BigDecimal.valueOf(-2), BigDecimal.ONE);
BigDecimalSummaryStatistics bds = list.stream().collect(BigDecimalSummaryStatistics.statistics());
System.out.println(bds);
System.out.println("average: "+bds.getAverage(MathContext.DECIMAL128));



3 elements between -2 and 1, sum=-1
average: -0.3333333333333333333333333333333333

这篇关于BigDecimal摘要统计信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆