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

Print this page




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




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