## src/share/classes/java/util/stream/Collectors.java

 ``` `````` 489 490 /** 491 * Returns a {@code Collector} that produces the sum of a double-valued 492 * function applied to the input elements. If no elements are present, 493 * the result is 0. 494 * 495 *
The sum returned can vary depending upon the order in which 496 * values are recorded, due to accumulated rounding error in 497 * addition of values of differing magnitudes. Values sorted by increasing 498 * absolute magnitude tend to yield more accurate results. If any recorded 499 * value is a {@code NaN} or the sum is at any point a {@code NaN} then the 500 * sum will be {@code NaN}. 501 * 502 * @param the type of the input elements 503 * @param mapper a function extracting the property to be summed 504 * @return a {@code Collector} that produces the sum of a derived property 505 */ 506 public static Collector 507 summingDouble(ToDoubleFunction mapper) { 508 return new CollectorImpl<>( 509 () -> new double, 510 (a, t) -> { a += mapper.applyAsDouble(t); }, 511 (a, b) -> { a += b; return a; }, 512 a -> a, CH_NOID); 513 } 514 515 /** 516 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued 517 * function applied to the input elements. If no elements are present, 518 * the result is 0. 519 * 520 * @param the type of the input elements 521 * @param mapper a function extracting the property to be summed 522 * @return a {@code Collector} that produces the sum of a derived property 523 */ 524 public static Collector 525 averagingInt(ToIntFunction mapper) { 526 return new CollectorImpl<>( 527 () -> new long, 528 (a, t) -> { a += mapper.applyAsInt(t); a++; }, 529 (a, b) -> { a += b; a += b; return a; }, 530 a -> (a == 0) ? 0.0d : (double) a / a, CH_NOID); 531 } 532 533 /** 534 * Returns a {@code Collector} that produces the arithmetic mean of a long-valued `````` 550 551 /** 552 * Returns a {@code Collector} that produces the arithmetic mean of a double-valued 553 * function applied to the input elements. If no elements are present, 554 * the result is 0. 555 * 556 *
The average returned can vary depending upon the order in which 557 * values are recorded, due to accumulated rounding error in 558 * addition of values of differing magnitudes. Values sorted by increasing 559 * absolute magnitude tend to yield more accurate results. If any recorded 560 * value is a {@code NaN} or the sum is at any point a {@code NaN} then the 561 * average will be {@code NaN}. 562 * 563 * @param the type of the input elements 564 * @param mapper a function extracting the property to be summed 565 * @return a {@code Collector} that produces the sum of a derived property 566 */ 567 public static Collector 568 averagingDouble(ToDoubleFunction mapper) { 569 return new CollectorImpl<>( 570 () -> new double, 571 (a, t) -> { a += mapper.applyAsDouble(t); a++; }, 572 (a, b) -> { a += b; a += b; return a; }, 573 a -> (a == 0) ? 0.0d : a / a, CH_NOID); 574 } 575 576 /** 577 * Returns a {@code Collector} which performs a reduction of its 578 * input elements under a specified {@code BinaryOperator} using the 579 * provided identity. 580 * 581 * @apiNote 582 * The {@code reducing()} collectors are most useful when used in a 583 * multi-level reduction, downstream of {@code groupingBy} or 584 * {@code partitioningBy}. To perform a simple reduction on a stream, 585 * use {@link Stream#reduce(Object, BinaryOperator)}} instead. 586 * 587 * @param element type for the input and output of the reduction 588 * @param identity the identity value for the reduction (also, the value 589 * that is returned when there are no input elements) 590 * @param op a {@code BinaryOperator} used to reduce the input elements 591 * @return a {@code Collector} which implements the reduction operation 592 * 593 * @see #reducing(BinaryOperator) ``` ``` `````` 489 490 /** 491 * Returns a {@code Collector} that produces the sum of a double-valued 492 * function applied to the input elements. If no elements are present, 493 * the result is 0. 494 * 495 *
The sum returned can vary depending upon the order in which 496 * values are recorded, due to accumulated rounding error in 497 * addition of values of differing magnitudes. Values sorted by increasing 498 * absolute magnitude tend to yield more accurate results. If any recorded 499 * value is a {@code NaN} or the sum is at any point a {@code NaN} then the 500 * sum will be {@code NaN}. 501 * 502 * @param the type of the input elements 503 * @param mapper a function extracting the property to be summed 504 * @return a {@code Collector} that produces the sum of a derived property 505 */ 506 public static Collector 507 summingDouble(ToDoubleFunction mapper) { 508 return new CollectorImpl<>( 509 () -> new double, 510 (a, t) -> { sumWithCompensation(a, mapper.applyAsDouble(t)); }, 511 (a, b) -> { sumWithCompensation(a, b); return sumWithCompensation(a, b); }, 512 a -> a, 513 CH_NOID); 514 } 515 516 /** 517 * Incorporate a new double value using Kahan summation / 518 * compensation summation. 519 * 520 * High-order bits of the sum are in intermediateSum, low-order 521 * bits of the sum are in intermediateSum, any additional 522 * elements are application-specific. 523 * 524 * @param intermediateSum the high-order and low-order words of the intermediate sum 525 * @param value the name value to be included in the running sum 526 */ 527 static double[] sumWithCompensation(double[] intermediateSum, double value) { 528 double tmp = value - intermediateSum; 529 double sum = intermediateSum; 530 double velvel = sum + tmp; // Little wolf of rounding error 531 intermediateSum = (velvel - sum) - tmp; 532 intermediateSum = velvel; 533 return intermediateSum; 534 } 535 536 537 /** 538 * Returns a {@code Collector} that produces the arithmetic mean of an integer-valued 539 * function applied to the input elements. If no elements are present, 540 * the result is 0. 541 * 542 * @param the type of the input elements 543 * @param mapper a function extracting the property to be summed 544 * @return a {@code Collector} that produces the sum of a derived property 545 */ 546 public static Collector 547 averagingInt(ToIntFunction mapper) { 548 return new CollectorImpl<>( 549 () -> new long, 550 (a, t) -> { a += mapper.applyAsInt(t); a++; }, 551 (a, b) -> { a += b; a += b; return a; }, 552 a -> (a == 0) ? 0.0d : (double) a / a, CH_NOID); 553 } 554 555 /** 556 * Returns a {@code Collector} that produces the arithmetic mean of a long-valued `````` 572 573 /** 574 * Returns a {@code Collector} that produces the arithmetic mean of a double-valued 575 * function applied to the input elements. If no elements are present, 576 * the result is 0. 577 * 578 *