@Override public DoubleSummaryStatistics summaryStatistics() { return collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine); }
/** * Returns a {@link String} which is the concatenation of the results of * calling {@link String#valueOf(double)} on each element of this stream, * separated by the specified delimiter, in encounter order. * * <p> * This is a terminal operation. * * @param delimiter the delimiter to be used between each element * @return the result of concatenation. For empty input stream empty String * is returned. * @since 0.3.1 */ public String joining(CharSequence delimiter) { return collect(DoubleCollector.joining(delimiter)); }
/** * Returns a {@link String} which is the concatenation of the results of * calling {@link String#valueOf(double)} on each element of this stream, * separated by the specified delimiter, with the specified prefix and * suffix in encounter order. * * <p> * This is a terminal operation. * * @param delimiter the delimiter to be used between each element * @param prefix the sequence of characters to be used at the beginning of * the joined result * @param suffix the sequence of characters to be used at the end of the * joined result * @return the result of concatenation. For empty input stream * {@code prefix + suffix} is returned. * @since 0.3.1 */ public String joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) { return collect(DoubleCollector.joining(delimiter, prefix, suffix)); }
/** * Returns the minimum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the lowest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public OptionalDouble minByDouble(DoubleUnaryOperator keyExtractor) { double[] result = collect(() -> new double[3], (acc, d) -> { double key = keyExtractor.applyAsDouble(d); if (acc[2] == 0 || Double.compare(acc[1], key) > 0) { acc[0] = d; acc[1] = key; acc[2] = 1; } }, (acc1, acc2) -> { if (acc2[2] == 1 && (acc1[2] == 0 || Double.compare(acc1[1], acc2[1]) > 0)) System.arraycopy(acc2, 0, acc1, 0, 3); }); return result[2] == 1 ? OptionalDouble.of(result[0]) : OptionalDouble.empty(); }
/** * Returns the minimum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the lowest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public OptionalDouble minByLong(DoubleToLongFunction keyExtractor) { return collect(PrimitiveBox::new, (box, d) -> { long key = keyExtractor.applyAsLong(d); if (!box.b || box.l > key) { box.b = true; box.l = key; box.d = d; } }, PrimitiveBox.MIN_LONG).asDouble(); }
/** * Returns the maximum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the highest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public OptionalDouble maxByInt(DoubleToIntFunction keyExtractor) { return collect(PrimitiveBox::new, (box, d) -> { int key = keyExtractor.applyAsInt(d); if (!box.b || box.i < key) { box.b = true; box.i = key; box.d = d; } }, PrimitiveBox.MAX_INT).asDouble(); }
/** * Returns the minimum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the lowest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public OptionalDouble minByInt(DoubleToIntFunction keyExtractor) { return collect(PrimitiveBox::new, (box, d) -> { int key = keyExtractor.applyAsInt(d); if (!box.b || box.i > key) { box.b = true; box.i = key; box.d = d; } }, PrimitiveBox.MIN_INT).asDouble(); }
/** * Returns the maximum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the highest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public OptionalDouble maxByLong(DoubleToLongFunction keyExtractor) { return collect(PrimitiveBox::new, (box, d) -> { long key = keyExtractor.applyAsLong(d); if (!box.b || box.l < key) { box.b = true; box.l = key; box.d = d; } }, PrimitiveBox.MAX_LONG).asDouble(); }
/** * Returns the maximum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the highest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public OptionalDouble maxByDouble(DoubleUnaryOperator keyExtractor) { double[] result = collect(() -> new double[3], (acc, d) -> { double key = keyExtractor.applyAsDouble(d); if (acc[2] == 0 || Double.compare(acc[1], key) < 0) { acc[0] = d; acc[1] = key; acc[2] = 1; } }, (acc1, acc2) -> { if (acc2[2] == 1 && (acc1[2] == 0 || Double.compare(acc1[1], acc2[1]) < 0)) System.arraycopy(acc2, 0, acc1, 0, 3); }); return result[2] == 1 ? OptionalDouble.of(result[0]) : OptionalDouble.empty(); }
/** * Returns the minimum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param <V> the type of the {@code Comparable} sort key * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the lowest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public <V extends Comparable<? super V>> OptionalDouble minBy(DoubleFunction<V> keyExtractor) { ObjDoubleBox<V> result = collect(() -> new ObjDoubleBox<>(null, 0), (box, i) -> { V val = Objects.requireNonNull(keyExtractor.apply(i)); if (box.a == null || box.a.compareTo(val) > 0) { box.a = val; box.b = i; } }, (box1, box2) -> { if (box2.a != null && (box1.a == null || box1.a.compareTo(box2.a) > 0)) { box1.a = box2.a; box1.b = box2.b; } }); return result.a == null ? OptionalDouble.empty() : OptionalDouble.of(result.b); }
/** * Returns the maximum element of this stream according to the provided key * extractor function. * * <p> * This is a terminal operation. * * @param <V> the type of the {@code Comparable} sort key * @param keyExtractor a non-interfering, stateless function * @return an {@code OptionalDouble} describing the first element of this * stream for which the highest value was returned by key extractor, * or an empty {@code OptionalDouble} if the stream is empty * @since 0.1.2 */ public <V extends Comparable<? super V>> OptionalDouble maxBy(DoubleFunction<V> keyExtractor) { ObjDoubleBox<V> result = collect(() -> new ObjDoubleBox<>(null, 0), (box, i) -> { V val = Objects.requireNonNull(keyExtractor.apply(i)); if (box.a == null || box.a.compareTo(val) < 0) { box.a = val; box.b = i; } }, (box1, box2) -> { if (box2.a != null && (box1.a == null || box1.a.compareTo(box2.a) < 0)) { box1.a = box2.a; box1.b = box2.b; } }); return result.a == null ? OptionalDouble.empty() : OptionalDouble.of(result.b); }
/** * Performs a mutable reduction operation on the elements of this stream * using an {@link DoubleCollector} which encapsulates the supplier, * accumulator and merger functions making easier to reuse collection * strategies. * * <p> * Like {@link #reduce(double, DoubleBinaryOperator)}, {@code collect} * operations can be parallelized without requiring additional * synchronization. * * <p> * This is a terminal operation. * * @param <A> the intermediate accumulation type of the * {@code DoubleCollector} * @param <R> type of the result * @param collector the {@code DoubleCollector} describing the reduction * @return the result of the reduction * @see #collect(Supplier, ObjDoubleConsumer, BiConsumer) * @since 0.3.0 */ @SuppressWarnings("unchecked") public <A, R> R collect(DoubleCollector<A, R> collector) { if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) return (R) collect(collector.supplier(), collector.doubleAccumulator(), collector.merger()); return collector.finisher().apply(collect(collector.supplier(), collector.doubleAccumulator(), collector .merger())); }
/** * Returns a {@code float[]} array containing the elements of this stream * which are converted to floats using {@code (float)} cast operation. * * <p> * This is a terminal operation. * * @return an array containing the elements of this stream * @since 0.3.0 */ public float[] toFloatArray() { if (isParallel()) return collect(DoubleCollector.toFloatArray()); java.util.Spliterator.OfDouble spliterator = spliterator(); long size = spliterator.getExactSizeIfKnown(); FloatBuffer buf; if (size >= 0 && size <= Integer.MAX_VALUE) { buf = new FloatBuffer((int) size); spliterator.forEachRemaining((DoubleConsumer) buf::addUnsafe); } else { buf = new FloatBuffer(); spliterator.forEachRemaining((DoubleConsumer) buf::add); } return buf.toArray(); }