src/share/classes/java/util/LongSummaryStatistics.java

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


  29 
  30 /**
  31  * A state object for collecting statistics such as count, min, max, sum, and
  32  * average.
  33  *
  34  * <p>This class is designed to work with (though does not require)
  35  * {@linkplain java.util.stream streams}. For example, you can compute
  36  * summary statistics on a stream of longs with:
  37  * <pre> {@code
  38  * LongSummaryStatistics stats = longStream.collect(LongSummaryStatistics::new,
  39  *     LongSummaryStatistics::accept,
  40  *     LongSummaryStatistics::combine);
  41  * }</pre>
  42  *
  43  * <p>{@code LongSummaryStatistics} can be used as a
  44  * {@linkplain java.util.stream.Stream#reduce(java.util.function.BinaryOperator) reduction}
  45  * target for a {@linkplain java.util.stream.Stream stream}. For example:
  46  *
  47  * <pre> {@code
  48  * LongSummaryStatistics stats = people.stream()
  49  *     .collect(Collectors.toLongSummaryStatistics(Person::getAge));
  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 ages in milliseconds.
  54  *
  55  * @implNote This implementation is not thread safe. However, it is safe to use
  56  * {@link java.util.stream.Collectors#toLongSummaryStatistics(java.util.function.ToLongFunction)
  57  * Collectors.toLongStatistics()} 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  *
  62  * <p>This implementation does not check for overflow of the sum.
  63  * @since 1.8
  64  */
  65 public class LongSummaryStatistics implements LongConsumer, IntConsumer {
  66     private long count;
  67     private long sum;
  68     private long min = Long.MAX_VALUE;
  69     private long max = Long.MIN_VALUE;
  70 
  71     /**
  72      * Construct an empty instance with zero count, zero sum,
  73      * {@code Long.MAX_VALUE} min, {@code Long.MIN_VALUE} max and zero
  74      * average.
  75      */
  76     public LongSummaryStatistics() { }




  29 
  30 /**
  31  * A state object for collecting statistics such as count, min, max, sum, and
  32  * average.
  33  *
  34  * <p>This class is designed to work with (though does not require)
  35  * {@linkplain java.util.stream streams}. For example, you can compute
  36  * summary statistics on a stream of longs with:
  37  * <pre> {@code
  38  * LongSummaryStatistics stats = longStream.collect(LongSummaryStatistics::new,
  39  *     LongSummaryStatistics::accept,
  40  *     LongSummaryStatistics::combine);
  41  * }</pre>
  42  *
  43  * <p>{@code LongSummaryStatistics} can be used as a
  44  * {@linkplain java.util.stream.Stream#reduce(java.util.function.BinaryOperator) reduction}
  45  * target for a {@linkplain java.util.stream.Stream stream}. For example:
  46  *
  47  * <pre> {@code
  48  * LongSummaryStatistics stats = people.stream()
  49  *     .collect(Collectors.summarizingLong(Person::getAge));
  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 ages in milliseconds.
  54  *
  55  * @implNote This implementation is not thread safe. However, it is safe to use
  56  * {@link java.util.stream.Collectors#summarizingLong(java.util.function.ToLongFunction)
  57  * Collectors.toLongStatistics()} 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  *
  62  * <p>This implementation does not check for overflow of the sum.
  63  * @since 1.8
  64  */
  65 public class LongSummaryStatistics implements LongConsumer, IntConsumer {
  66     private long count;
  67     private long sum;
  68     private long min = Long.MAX_VALUE;
  69     private long max = Long.MIN_VALUE;
  70 
  71     /**
  72      * Construct an empty instance with zero count, zero sum,
  73      * {@code Long.MAX_VALUE} min, {@code Long.MIN_VALUE} max and zero
  74      * average.
  75      */
  76     public LongSummaryStatistics() { }