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

Print this page
rev 7597 : 8015318: Extend Collector with 'finish' operation
Reviewed-by:
Contributed-by: brian.goetz@oracle.com


  28 
  29 /**
  30  * A state object for collecting statistics such as count, min, max, sum, and
  31  * average.
  32  *
  33  * <p>This class is designed to work with (though does not require)
  34  * {@linkplain java.util.stream streams}. For example, you can compute
  35  * summary statistics on a stream of doubles with:
  36  * <pre> {@code
  37  * DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new,
  38  *     DoubleSummaryStatistics::accept,
  39  *     DoubleSummaryStatistics::combine);
  40  * }</pre>
  41  *
  42  * <p>{@code DoubleSummaryStatistics} can be used as a
  43  * {@linkplain java.util.stream.Stream#reduce(java.util.function.BinaryOperator) reduction}
  44  * target for a {@linkplain java.util.stream.Stream stream}. For example:
  45  *
  46  * <pre> {@code
  47  * DoubleSummaryStatistics stats = people.stream()
  48  *     .collect(Collectors.toDoubleSummaryStatistics(Person::getWeight));
  49  *}</pre>
  50  *
  51  * This computes, in a single pass, the count of people, as well as the minimum,
  52  * maximum, sum, and average of their weights.
  53  *
  54  * @implNote This implementation is not thread safe. However, it is safe to use
  55  * {@link java.util.stream.Collectors#toDoubleSummaryStatistics(java.util.function.ToDoubleFunction)
  56  * Collectors.toDoubleStatistics()} on a parallel stream, because the parallel
  57  * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  58  * provides the necessary partitioning, isolation, and merging of results for
  59  * safe and efficient parallel execution.
  60  * @since 1.8
  61  */
  62 public class DoubleSummaryStatistics implements DoubleConsumer {
  63     private long count;
  64     private double sum;
  65     private double min = Double.POSITIVE_INFINITY;
  66     private double max = Double.NEGATIVE_INFINITY;
  67 
  68     /**
  69      * Construct an empty instance with zero count, zero sum,
  70      * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY}
  71      * max and zero average.
  72      */
  73     public DoubleSummaryStatistics() { }
  74 
  75     /**




  28 
  29 /**
  30  * A state object for collecting statistics such as count, min, max, sum, and
  31  * average.
  32  *
  33  * <p>This class is designed to work with (though does not require)
  34  * {@linkplain java.util.stream streams}. For example, you can compute
  35  * summary statistics on a stream of doubles with:
  36  * <pre> {@code
  37  * DoubleSummaryStatistics stats = doubleStream.collect(DoubleSummaryStatistics::new,
  38  *     DoubleSummaryStatistics::accept,
  39  *     DoubleSummaryStatistics::combine);
  40  * }</pre>
  41  *
  42  * <p>{@code DoubleSummaryStatistics} can be used as a
  43  * {@linkplain java.util.stream.Stream#reduce(java.util.function.BinaryOperator) reduction}
  44  * target for a {@linkplain java.util.stream.Stream stream}. For example:
  45  *
  46  * <pre> {@code
  47  * DoubleSummaryStatistics stats = people.stream()
  48  *     .collect(Collectors.summarizingDouble(Person::getWeight));
  49  *}</pre>
  50  *
  51  * This computes, in a single pass, the count of people, as well as the minimum,
  52  * maximum, sum, and average of their weights.
  53  *
  54  * @implNote This implementation is not thread safe. However, it is safe to use
  55  * {@link java.util.stream.Collectors#summarizingDouble(java.util.function.ToDoubleFunction)
  56  * Collectors.toDoubleStatistics()} on a parallel stream, because the parallel
  57  * implementation of {@link java.util.stream.Stream#collect Stream.collect()}
  58  * provides the necessary partitioning, isolation, and merging of results for
  59  * safe and efficient parallel execution.
  60  * @since 1.8
  61  */
  62 public class DoubleSummaryStatistics implements DoubleConsumer {
  63     private long count;
  64     private double sum;
  65     private double min = Double.POSITIVE_INFINITY;
  66     private double max = Double.NEGATIVE_INFINITY;
  67 
  68     /**
  69      * Construct an empty instance with zero count, zero sum,
  70      * {@code Double.POSITIVE_INFINITY} min, {@code Double.NEGATIVE_INFINITY}
  71      * max and zero average.
  72      */
  73     public DoubleSummaryStatistics() { }
  74 
  75     /**