src/share/classes/java/util/stream/DoublePipeline.java

Print this page




 360     public final DoubleStream distinct() {
 361         // While functional and quick to implement, this approach is not very efficient.
 362         // An efficient version requires a double-specific map/set implementation.
 363         return boxed().distinct().mapToDouble(i -> (double) i);
 364     }
 365 
 366     // Terminal ops from DoubleStream
 367 
 368     @Override
 369     public void forEach(DoubleConsumer consumer) {
 370         evaluate(ForEachOps.makeDouble(consumer, false));
 371     }
 372 
 373     @Override
 374     public void forEachOrdered(DoubleConsumer consumer) {
 375         evaluate(ForEachOps.makeDouble(consumer, true));
 376     }
 377 
 378     @Override
 379     public final double sum() {
 380         // TODO: better algorithm to compensate for errors
 381         return reduce(0.0, Double::sum);















 382     }
 383 
 384     @Override
 385     public final OptionalDouble min() {
 386         return reduce(Math::min);
 387     }
 388 
 389     @Override
 390     public final OptionalDouble max() {
 391         return reduce(Math::max);
 392     }
 393 









 394     @Override
 395     public final OptionalDouble average() {
 396         double[] avg = collect(() -> new double[2],
 397                                (ll, i) -> {
 398                                    ll[0]++;
 399                                    ll[1] += i;






 400                                },
 401                                (ll, rr) -> {
 402                                    ll[0] += rr[0];
 403                                    ll[1] += rr[1];

 404                                });
 405         return avg[0] > 0
 406                ? OptionalDouble.of(avg[1] / avg[0])

 407                : OptionalDouble.empty();
 408     }
 409 
 410     @Override
 411     public final long count() {
 412         return mapToObj(e -> null).mapToInt(e -> 1).sum();
 413     }
 414 
 415     @Override
 416     public final DoubleSummaryStatistics summaryStatistics() {
 417         return collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept,
 418                        DoubleSummaryStatistics::combine);
 419     }
 420 
 421     @Override
 422     public final double reduce(double identity, DoubleBinaryOperator op) {
 423         return evaluate(ReduceOps.makeDouble(identity, op));
 424     }
 425 
 426     @Override




 360     public final DoubleStream distinct() {
 361         // While functional and quick to implement, this approach is not very efficient.
 362         // An efficient version requires a double-specific map/set implementation.
 363         return boxed().distinct().mapToDouble(i -> (double) i);
 364     }
 365 
 366     // Terminal ops from DoubleStream
 367 
 368     @Override
 369     public void forEach(DoubleConsumer consumer) {
 370         evaluate(ForEachOps.makeDouble(consumer, false));
 371     }
 372 
 373     @Override
 374     public void forEachOrdered(DoubleConsumer consumer) {
 375         evaluate(ForEachOps.makeDouble(consumer, true));
 376     }
 377 
 378     @Override
 379     public final double sum() {
 380         /*
 381          * In the arrays allocated for the collect operation, index 0
 382          * holds the high-order bits of the running sum and index 1
 383          * holds the low-order bits of the sum computed via
 384          * compensated summation.
 385          */
 386         double[] summation = collect(() -> new double[2],
 387                                (ll, d) -> {
 388                                    Collectors.sumWithCompensation(ll, d);
 389                                },
 390                                (ll, rr) -> {
 391                                    Collectors.sumWithCompensation(ll, rr[0]);
 392                                    Collectors.sumWithCompensation(ll, rr[1]);
 393                                });
 394                 
 395         // Better error bounds to add both terms as the final sum
 396         return summation[0] + summation[1];
 397     }
 398 
 399     @Override
 400     public final OptionalDouble min() {
 401         return reduce(Math::min);
 402     }
 403 
 404     @Override
 405     public final OptionalDouble max() {
 406         return reduce(Math::max);
 407     }
 408 
 409     /**
 410      * {@inheritDoc}
 411      *
 412      * @implNote The {@code double} format can represent all
 413      * consecutive integers in the range -2<sup>53</sup> to
 414      * 2<sup>53</sup>. If the pipeline has more than 2<sup>53</sup>
 415      * values, the divisor in the average computation will saturate at
 416      * 2<sup>53</sup>, leading to additional numerical errors.
 417      */
 418     @Override
 419     public final OptionalDouble average() {
 420         /*
 421          * In the arrays allocated for the collect operation, index 0
 422          * holds the high-order bits of the running sum, index 1 holds
 423          * the low-order bits of the sum computed via compensated
 424          * summation, and index 2 holds the number of values seen.
 425          */
 426         double[] avg = collect(() -> new double[3],
 427                                (ll, d) -> {
 428                                    ll[2]++;
 429                                    Collectors.sumWithCompensation(ll, d);
 430                                },
 431                                (ll, rr) -> {
 432                                    Collectors.sumWithCompensation(ll, rr[0]);
 433                                    Collectors.sumWithCompensation(ll, rr[1]);
 434                                    ll[2] += rr[2];
 435                                });
 436         return avg[2] > 0
 437             // Better error bounds to add both terms as the final sum to compute average
 438             ? OptionalDouble.of((avg[0] + avg[1]) / avg[2])
 439             : OptionalDouble.empty();
 440     }
 441 
 442     @Override
 443     public final long count() {
 444         return mapToObj(e -> null).mapToInt(e -> 1).sum();
 445     }
 446 
 447     @Override
 448     public final DoubleSummaryStatistics summaryStatistics() {
 449         return collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept,
 450                        DoubleSummaryStatistics::combine);
 451     }
 452 
 453     @Override
 454     public final double reduce(double identity, DoubleBinaryOperator op) {
 455         return evaluate(ReduceOps.makeDouble(identity, op));
 456     }
 457 
 458     @Override