src/share/classes/java/util/DoubleSummaryStatistics.java

Print this page




  46  *
  47  * <pre> {@code
  48  * DoubleSummaryStatistics stats = people.stream()
  49  *     .collect(Collectors.summarizingDouble(Person::getWeight));
  50  *}</pre>
  51  *
  52  * This computes, in a single pass, the count of people, as well as the minimum,
  53  * maximum, sum, and average of their weights.
  54  *
  55  * @implNote This implementation is not thread safe. However, it is safe to use
  56  * {@link java.util.stream.Collectors#summarizingDouble(java.util.function.ToDoubleFunction)
  57  * Collectors.toDoubleStatistics()} on a parallel stream, because the parallel
  58  * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  59  * provides the necessary partitioning, isolation, and merging of results for
  60  * safe and efficient parallel execution.
  61  * @since 1.8
  62  */
  63 public class DoubleSummaryStatistics implements DoubleConsumer {
  64     private long count;
  65     private double sum;

  66     private double min = Double.POSITIVE_INFINITY;
  67     private double max = Double.NEGATIVE_INFINITY;
  68 
  69     /**
  70      * Construct an empty instance with zero count, zero sum,
  71      * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY}
  72      * max and zero average.
  73      */
  74     public DoubleSummaryStatistics() { }
  75 
  76     /**
  77      * Records another value into the summary information.
  78      *
  79      * @param value the input value
  80      */
  81     @Override
  82     public void accept(double value) {
  83         ++count;
  84         sum += value;
  85         min = Math.min(min, value);
  86         max = Math.max(max, value);
  87     }
  88 
  89     /**
  90      * Combines the state of another {@code DoubleSummaryStatistics} into this
  91      * one.
  92      *
  93      * @param other another {@code DoubleSummaryStatistics}
  94      * @throws NullPointerException if {@code other} is null
  95      */
  96     public void combine(DoubleSummaryStatistics other) {
  97         count += other.count;
  98         sum += other.sum;

  99         min = Math.min(min, other.min);
 100         max = Math.max(max, other.max);











 101     }
 102 
 103     /**
 104      * Return the count of values recorded.
 105      *
 106      * @return the count of values
 107      */
 108     public final long getCount() {
 109         return count;
 110     }
 111 
 112     /**
 113      * Returns the sum of values recorded, or zero if no values have been
 114      * recorded.
 115      *
 116      * If any recorded value is a NaN or the sum is at any point a NaN
 117      * then the sum will be NaN.
 118      *
 119      * <p> The value of a floating-point sum is a function both of the
 120      * input values as well as the order of addition operations. The




  46  *
  47  * <pre> {@code
  48  * DoubleSummaryStatistics stats = people.stream()
  49  *     .collect(Collectors.summarizingDouble(Person::getWeight));
  50  *}</pre>
  51  *
  52  * This computes, in a single pass, the count of people, as well as the minimum,
  53  * maximum, sum, and average of their weights.
  54  *
  55  * @implNote This implementation is not thread safe. However, it is safe to use
  56  * {@link java.util.stream.Collectors#summarizingDouble(java.util.function.ToDoubleFunction)
  57  * Collectors.toDoubleStatistics()} on a parallel stream, because the parallel
  58  * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  59  * provides the necessary partitioning, isolation, and merging of results for
  60  * safe and efficient parallel execution.
  61  * @since 1.8
  62  */
  63 public class DoubleSummaryStatistics implements DoubleConsumer {
  64     private long count;
  65     private double sum;
  66     private double sumCompensation; // Low order bits of sum
  67     private double min = Double.POSITIVE_INFINITY;
  68     private double max = Double.NEGATIVE_INFINITY;
  69 
  70     /**
  71      * Construct an empty instance with zero count, zero sum,
  72      * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY}
  73      * max and zero average.
  74      */
  75     public DoubleSummaryStatistics() { }
  76 
  77     /**
  78      * Records another value into the summary information.
  79      *
  80      * @param value the input value
  81      */
  82     @Override
  83     public void accept(double value) {
  84         ++count;
  85         sumWithCompensation(value);
  86         min = Math.min(min, value);
  87         max = Math.max(max, value);
  88     }
  89 
  90     /**
  91      * Combines the state of another {@code DoubleSummaryStatistics} into this
  92      * one.
  93      *
  94      * @param other another {@code DoubleSummaryStatistics}
  95      * @throws NullPointerException if {@code other} is null
  96      */
  97     public void combine(DoubleSummaryStatistics other) {
  98         count += other.count;
  99         sumWithCompensation(other.sum);
 100         sumWithCompensation(other.sumCompensation);
 101         min = Math.min(min, other.min);
 102         max = Math.max(max, other.max);
 103     }
 104 
 105     /**
 106      * Incorporate a new double value using Kahan summation /
 107      * compensation summation.
 108      */
 109     private void sumWithCompensation(double value) {
 110         double tmp = value - sumCompensation;
 111         double velvel = sum + tmp; // Little wolf of rounding error
 112         sumCompensation = (velvel - sum) - tmp;
 113         sum = velvel;
 114     }
 115 
 116     /**
 117      * Return the count of values recorded.
 118      *
 119      * @return the count of values
 120      */
 121     public final long getCount() {
 122         return count;
 123     }
 124 
 125     /**
 126      * Returns the sum of values recorded, or zero if no values have been
 127      * recorded.
 128      *
 129      * If any recorded value is a NaN or the sum is at any point a NaN
 130      * then the sum will be NaN.
 131      *
 132      * <p> The value of a floating-point sum is a function both of the
 133      * input values as well as the order of addition operations. The