Java : DoubleStream with Examples

DoubleStream (Java SE 21 & JDK 21) with Examples.
You will find code examples on most DoubleStream methods.


Summary

A sequence of primitive double-valued elements supporting sequential and parallel aggregate operations. This is the double primitive specialization of Stream.

Class diagram

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

System.out.println("-- forEach --");
stream.forEach(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//0.1
//0.2
//0.3
final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var ret = stream.map(v -> v * 2.0).toArray();
System.out.println(Arrays.toString(ret)); // [0.2, 0.4, 0.6]

Methods

boolean allMatch (DoublePredicate predicate)

Returns whether all elements of this stream match the provided predicate.

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        // Check if the number is negative.
        return value < 0.0;
    }
};

{
    final var stream = DoubleStream.of(-0.1, -0.2, -0.3);

    final var ret = stream.allMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = DoubleStream.of(1.0, -2.0, 3.0, -4.0);

    final var ret = stream.allMatch(predicate);
    System.out.println(ret); // false
}

boolean anyMatch (DoublePredicate predicate)

Returns whether any elements of this stream match the provided predicate.

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        // Check if the number is negative.
        return value < 0.0;
    }
};

{
    final var stream = DoubleStream.of(-0.1, -0.2, -0.3);

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = DoubleStream.of(0.1, -0.2, 0.3);

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // true
}
{
    final var stream = DoubleStream.of(0.1, 0.2, 0.3);

    final var ret = stream.anyMatch(predicate);
    System.out.println(ret); // false
}

OptionalDouble average ()

Returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty.

final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);

final var opt = stream.average();
opt.ifPresent(value -> {
    System.out.println(value); // 3.5
});
final var opt = DoubleStream.empty().average();
System.out.println(opt.isEmpty()); // true

Stream<Double> boxed ()

Returns a Stream consisting of the elements of this stream, boxed to Double.

final Stream<Double> stream = DoubleStream.of(0.1, 0.2, 0.3).boxed();

final var ret = stream.toList();
System.out.println(ret); // [0.1, 0.2, 0.3]

static DoubleStream.Builder builder ()

Returns a builder for a DoubleStream.

final var builder = DoubleStream.builder();
builder.add(0.1).add(0.2).add(0.3);

final var stream = builder.build();

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]

<R> R collect (Supplier<R> supplier, ObjDoubleConsumer<R> accumulator, BiConsumer<R,R> combiner)

Performs a mutable reduction operation on the elements of this stream.

final var supplier = new Supplier<List<String>>() {
    @Override
    public List<String> get() {
        System.out.println("-- supplier --");
        return new ArrayList<>();
    }
};

final var accumulator = new ObjDoubleConsumer<List<String>>() {
    @Override
    public void accept(List<String> list, double value) {
        System.out.println("-- accumulator --");
        System.out.println("  list = " + list);
        System.out.println("  value = " + value);

        list.add("%.2e".formatted(value));
    }
};

final var combiner = new BiConsumer<List<String>, List<String>>() {
    @Override
    public void accept(List<String> list1, List<String> list2) {
        System.out.println("-- combiner --");
        System.out.println("  list1 = " + list1);
        System.out.println("  list2 = " + list2);

        list1.addAll(list2);
    }
};

{
    final var stream = DoubleStream.of(0.1, 0.2, 0.3);

    final var ret = stream.collect(supplier, accumulator, combiner);
    System.out.println("collect = " + ret);

    // Result
    // ↓
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 0.1
    //-- accumulator --
    //  list = [1.00e-01]
    //  value = 0.2
    //-- accumulator --
    //  list = [1.00e-01, 2.00e-01]
    //  value = 0.3
    //collect = [1.00e-01, 2.00e-01, 3.00e-01]
}
{
    final var parallelStream = DoubleStream.of(0.1, 0.2, 0.3).parallel();
    System.out.println(parallelStream.isParallel()); // true

    final var ret = parallelStream.collect(supplier, accumulator, combiner);
    System.out.println("collect = " + ret);

    // Result
    // ↓
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 0.2
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 0.3
    //-- supplier --
    //-- accumulator --
    //  list = []
    //  value = 0.1
    //-- combiner --
    //  list1 = [2.00e-01]
    //  list2 = [3.00e-01]
    //-- combiner --
    //  list1 = [1.00e-01]
    //  list2 = [2.00e-01, 3.00e-01]
    //collect = [1.00e-01, 2.00e-01, 3.00e-01]
}

static DoubleStream concat (DoubleStream a, DoubleStream b)

Creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

final var stream1 = DoubleStream.of(0.1, 0.2, 0.3);
final var stream2 = DoubleStream.of(7.0, 8.0, 9.0);

System.out.println(stream1.isParallel()); // false
System.out.println(stream2.isParallel()); // false

final var concatStream = DoubleStream.concat(stream1, stream2);

System.out.println(concatStream.isParallel()); // false

final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 7.0, 8.0, 9.0]
final var stream1 = DoubleStream.of(0.1, 0.2, 0.3).parallel();
final var stream2 = DoubleStream.of(7.0, 8.0, 9.0).parallel();

System.out.println(stream1.isParallel()); // true
System.out.println(stream2.isParallel()); // true

final var concatStream = DoubleStream.concat(stream1, stream2);

System.out.println(concatStream.isParallel()); // true

final var ret = concatStream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 7.0, 8.0, 9.0]

long count ()

Returns the count of elements in this stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4);
System.out.println(stream.count()); // 4
final var stream = DoubleStream.empty();
System.out.println(stream.count()); // 0

DoubleStream distinct ()

Returns a stream consisting of the distinct elements of this stream.

final var stream = DoubleStream.of(1.0, 2.0, 2.0, 3.0, 3.0, 3.0);

final var ret = stream.distinct().toArray();
System.out.println(Arrays.toString(ret)); // [1.0, 2.0, 3.0]

default DoubleStream dropWhile (DoublePredicate predicate)

Returns, if this stream is ordered, a stream consisting of the remaining elements of this stream after dropping the longest prefix of elements that match the given predicate.

final var stream = DoubleStream.of(1.0, 2.0, Double.NaN, 4.0, 5.0);

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return !Double.isNaN(value);
    }
};

final var ret = stream.dropWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [NaN, 4.0, 5.0]
final var stream = DoubleStream.of(-1.0, -2.0, -3.0, Double.NaN, -5.0);

final var ret = stream.dropWhile(v -> !Double.isNaN(v)).toArray();
System.out.println(Arrays.toString(ret)); // [NaN, -5.0]

static DoubleStream empty ()

Returns an empty sequential DoubleStream.

final var stream = DoubleStream.empty();
System.out.println(stream.count()); // 0
final var stream = DoubleStream.empty();
final var array = stream.toArray();
System.out.println(Arrays.toString(array)); // []

DoubleStream filter (DoublePredicate predicate)

Returns a stream consisting of the elements of this stream that match the given predicate.

final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        // Check if the number is negative.
        return value < 0.0;
    }
};

final var ret = stream.filter(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [-0.2, -0.4]
final var stream = DoubleStream.of(0.1, -0.2, 0.3, -0.4);

final var ret = stream.filter(v -> v > 0.0).toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.3]

OptionalDouble findAny ()

Returns an OptionalDouble describing some element of the stream, or an empty OptionalDouble if the stream is empty.

int count1 = 0;
int count2 = 0;
int count3 = 0;

for (int i = 0; i < 1000; i++) {

    final var stream = DoubleStream.of(1.0, 2.0, 3.0).parallel();
    System.out.println(stream.isParallel()); // true

    // 1.0 or 2.0 or 3.0
    final var opt = stream.findAny();
    if (opt.isEmpty()) {
        throw new IllegalStateException();
    }

    final var value = Math.round(opt.orElseThrow());
    if (value == 1) {
        count1++;
    } else if (value == 2) {
        count2++;
    } else if (value == 3) {
        count3++;
    }
}

System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);

// Result
// ↓
//count1 : 35
//count2 : 963
//count3 : 2
final var stream = DoubleStream.empty();

final var opt = stream.findAny();
System.out.println(opt.isEmpty()); // true

OptionalDouble findFirst ()

Returns an OptionalDouble describing the first element of this stream, or an empty OptionalDouble if the stream is empty.

final var stream = DoubleStream.of(1.0, 2.0, 3.0);

final var opt = stream.findFirst();
opt.ifPresent(value -> {
    System.out.println(value); // 1.0
});
// parallel
final var stream = DoubleStream.of(1.0, 2.0, 3.0).parallel();
System.out.println(stream.isParallel()); // true

final var opt = stream.findFirst();
opt.ifPresent(value -> {
    System.out.println(value); // 1.0
});
int count1 = 0;
int count2 = 0;
int count3 = 0;

for (int i = 0; i < 1000; i++) {

    // unordered and parallel
    final var stream = DoubleStream.of(1.0, 2.0, 3.0).unordered().parallel();

    // 1 or 2 or 3
    final var opt = stream.findFirst();
    if (opt.isEmpty()) {
        throw new IllegalStateException();
    }

    final var value = Math.round(opt.orElseThrow());
    if (value == 1) {
        count1++;
    } else if (value == 2) {
        count2++;
    } else if (value == 3) {
        count3++;
    }
}

System.out.println("count1 : " + count1);
System.out.println("count2 : " + count2);
System.out.println("count3 : " + count3);

// Result
// ↓
//count1 : 79
//count2 : 919
//count3 : 2
final var stream = DoubleStream.empty();

final var opt = stream.findFirst();
System.out.println(opt.isEmpty()); // true

DoubleStream flatMap (DoubleFunction<? extends DoubleStream> mapper)

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

final var stream = DoubleStream.of(1.0, 2.0, 3.0);

final var mapper = new DoubleFunction<DoubleStream>() {
    @Override
    public DoubleStream apply(double value) {
        return DoubleStream.of(value, value + 0.1, value + 0.2);
    }
};

final var ret = stream.flatMap(mapper).toArray();

// [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2]
System.out.println(Arrays.toString(ret));

void forEach (DoubleConsumer action)

Performs an action for each element of this stream.

Please see also : forEachOrdered(DoubleConsumer action)

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var action = new DoubleConsumer() {
    @Override
    public void accept(double value) {
        System.out.println(value);
    }
};

System.out.println("-- forEach --");
stream.forEach(action);

// Result
// ↓
//-- forEach --
//0.1
//0.2
//0.3
// An example with a lambda expression.
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);

System.out.println("-- forEach --");
stream.forEach(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//-0.1
//-0.2
//-0.3
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5).parallel();
System.out.println(stream.isParallel()); // true

System.out.println("-- forEach --");
stream.forEach(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//0.3
//0.2
//0.1
//0.5
//0.4

void forEachOrdered (DoubleConsumer action)

Performs an action for each element of this stream, guaranteeing that each element is processed in encounter order for streams that have a defined encounter order.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var action = new DoubleConsumer() {
    @Override
    public void accept(double value) {
        System.out.println(value);
    }
};

System.out.println("-- forEach --");
stream.forEachOrdered(action);

// Result
// ↓
//-- forEach --
//0.1
//0.2
//0.3
final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5).parallel();
System.out.println(stream.isParallel()); // true

System.out.println("-- forEach --");
stream.forEachOrdered(v -> {
    System.out.println(v);
});

// Result
// ↓
//-- forEach --
//0.1
//0.2
//0.3
//0.4
//0.5

static DoubleStream generate (DoubleSupplier s)

Returns an infinite sequential unordered stream where each element is generated by the provided DoubleSupplier.

final var supplier = new DoubleSupplier() {
    double count;

    @Override
    public double getAsDouble() {
        count += 10.0;
        return count;
    }
};

final var stream = DoubleStream.generate(supplier);

final var ret = stream.limit(5).toArray();
System.out.println(Arrays.toString(ret)); // [10.0, 20.0, 30.0, 40.0, 50.0]

static DoubleStream iterate (double seed, DoublePredicate hasNext, DoubleUnaryOperator next)

Returns a sequential ordered DoubleStream produced by iterative application of the given next function to an initial element, conditioned on satisfying the given hasNext predicate.

final var hasNext = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return value <= 100.0;
    }
};

final var next = new DoubleUnaryOperator() {
    @Override
    public double applyAsDouble(double operand) {
        return operand * 2.0;
    }
};

final var stream = DoubleStream.iterate(1.0, hasNext, next);
final var ret = stream.toArray();

// [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0]
System.out.println(Arrays.toString(ret));

static DoubleStream iterate (double seed, DoubleUnaryOperator f)

Returns an infinite sequential ordered DoubleStream produced by iterative application of a function f to an initial element seed, producing a Stream consisting of seed, f(seed), f(f(seed)), etc.

final var next = new DoubleUnaryOperator() {
    @Override
    public double applyAsDouble(double operand) {
        return operand * 2.0;
    }
};

final var stream = DoubleStream.iterate(1.0, next);
final var ret = stream.limit(10).toArray();

// [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0, 512.0]
System.out.println(Arrays.toString(ret));

DoubleStream limit (long maxSize)

Returns a stream consisting of the elements of this stream, truncated to be no longer than maxSize in length.

final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5);

final var ret = stream.limit(3).toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]

DoubleStream map (DoubleUnaryOperator mapper)

Returns a stream consisting of the results of applying the given function to the elements of this stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var mapper = new DoubleUnaryOperator() {
    @Override
    public double applyAsDouble(double operand) {
        return operand * 2.0;
    }
};

final var ret = stream.map(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [0.2, 0.4, 0.6]
final var stream = DoubleStream.of(1.0, 2.0, 3.0);

final var ret = stream.map(v -> v * 1.5).toArray();
System.out.println(Arrays.toString(ret)); // [1.5, 3.0, 4.5]

default DoubleStream mapMulti (DoubleStream.DoubleMapMultiConsumer mapper)

Returns a stream consisting of the results of replacing each element of this stream with multiple elements, specifically zero or more elements.

final var stream = DoubleStream.of(1.0, 2.0, 3.0);

final var mapper = new DoubleStream.DoubleMapMultiConsumer() {
    @Override
    public void accept(double value, DoubleConsumer dc) {
        dc.accept(value);
        dc.accept(value + 0.1);
        dc.accept(value + 0.2);
    }
};

final var ret = stream.mapMulti(mapper).toArray();

// [1.0, 1.1, 1.2, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2]
System.out.println(Arrays.toString(ret));

IntStream mapToInt (DoubleToIntFunction mapper)

Returns an IntStream consisting of the results of applying the given function to the elements of this stream.

final var stream = DoubleStream.of(1.1, 2.2, 3.3);

final var mapper = new DoubleToIntFunction() {
    @Override
    public int applyAsInt(double value) {
        final var rounded = Math.round(value);
        return Math.toIntExact(rounded);
    }
};

final var ret = stream.mapToInt(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [1, 2, 3]

LongStream mapToLong (DoubleToLongFunction mapper)

Returns a LongStream consisting of the results of applying the given function to the elements of this stream.

final var stream = DoubleStream.of(1.0e10, 2.0e10);

final var mapper = new DoubleToLongFunction() {
    @Override
    public long applyAsLong(double value) {
        return Math.round(value);
    }
};

final var ret = stream.mapToLong(mapper).toArray();
System.out.println(Arrays.toString(ret)); // [10000000000, 20000000000]

<U> Stream<U> mapToObj (DoubleFunction<? extends U> mapper)

Returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var mapper = new DoubleFunction<String>() {
    @Override
    public String apply(double value) {
        return "num=" + value;
    }
};

final var ret = stream.mapToObj(mapper).toList();
System.out.println(ret); // [num=0.1, num=0.2, num=0.3]

OptionalDouble max ()

Returns an OptionalDouble describing the maximum element of this stream, or an empty OptionalDouble if this stream is empty.

final var stream = DoubleStream.of(7.0, 99.0, 0.1, 5.0);

final var opt = stream.max();
opt.ifPresent(value -> {
    System.out.println(value); // 99.0
});
final var stream = DoubleStream.empty();

final var opt = stream.max();
System.out.println(opt.isEmpty()); // true

OptionalDouble min ()

Returns an OptionalDouble describing the minimum element of this stream, or an empty OptionalDouble if this stream is empty.

final var stream = DoubleStream.of(7.0, 99.0, 0.1, 5.0);

final var opt = stream.min();
opt.ifPresent(value -> {
    System.out.println(value); // 0.1
});
final var stream = DoubleStream.empty();

final var opt = stream.min();
System.out.println(opt.isEmpty()); // true

boolean noneMatch (DoublePredicate predicate)

Returns whether no elements of this stream match the provided predicate.

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        // Check if the number is negative.
        return value < 0.0;
    }
};

{
    final var stream = DoubleStream.of(-0.1, -0.2, -0.3);

    final var ret = stream.noneMatch(predicate);
    System.out.println(ret); // false
}
{
    final var stream = DoubleStream.of(0.1, -0.2, 0.3);

    final var ret = stream.noneMatch(predicate);
    System.out.println(ret); // false
}
{
    final var stream = DoubleStream.of(0.1, 0.2, 0.3);

    final var ret = stream.noneMatch(predicate);
    System.out.println(ret); // true
}

static DoubleStream of (double t)

Returns a sequential DoubleStream containing a single element.

final var stream = DoubleStream.of(0.0);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.0]
final var stream = DoubleStream.of(1.23);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [1.23]

static DoubleStream of (double... values)

Returns a sequential ordered stream whose elements are the specified values.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var ret = stream.toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]

DoubleStream peek (DoubleConsumer action)

Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);

final var action = new DoubleConsumer() {
    @Override
    public void accept(double value) {
        System.out.println("peek : value = " + value);
    }
};

final var array = stream.peek(action).toArray();
System.out.println("array = " + Arrays.toString(array));

// Result
// ↓
//peek : value = 0.1
//peek : value = 0.2
//peek : value = 0.3
//array = [0.1, 0.2, 0.3]

double reduce (double identity, DoubleBinaryOperator op)

Performs a reduction on the elements of this stream, using the provided identity value and an associative accumulation function, and returns the reduced value.

final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0);
final var identity = 0.0;

final var op = new DoubleBinaryOperator() {
    @Override
    public double applyAsDouble(double left, double right) {
        if (identity == left) {
            return right;
        }

        return left + right;
    }
};

// 1.0 + 2.0 + 3.0 + 4.0 = 10.0
final var ret = stream.reduce(identity, op);
System.out.println(ret); // 10.0
final var stream = DoubleStream.empty();
final var identity = 0.0;

final var ret = stream.reduce(identity, (left, right) -> {
    if (identity == left) {
        return right;
    }

    return left + right;
});

System.out.println(ret); // 0.0

OptionalDouble reduce (DoubleBinaryOperator op)

Performs a reduction on the elements of this stream, using an associative accumulation function, and returns an OptionalDouble describing the reduced value, if any.

final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0);

final var op = new DoubleBinaryOperator() {
    @Override
    public double applyAsDouble(double left, double right) {
        return left + right;
    }
};

// 1.0 + 2.0 + 3.0 + 4.0 = 10.0
final var opt = stream.reduce(op);
opt.ifPresent(value -> {
    System.out.println(value); // 10.0
});
final var stream = DoubleStream.empty();

final var opt = stream.reduce((left, right) -> left + right);
System.out.println(opt.isEmpty()); // true

DoubleStream skip (long n)

Returns a stream consisting of the remaining elements of this stream after discarding the first n elements of the stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7);

final var ret = stream.skip(4).toArray();
System.out.println(Arrays.toString(ret)); // [0.5, 0.6, 0.7]

DoubleStream sorted ()

Returns a stream consisting of the elements of this stream in sorted order.

final var stream = DoubleStream.of(0.2, 0.5, 0.4, 0.1, 0.3);

final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 0.4, 0.5]
final var stream = DoubleStream.of(0.2, 0.5, 0.4, 0.1, 0.3).parallel();
System.out.println(stream.isParallel()); // true

final var ret = stream.sorted().toArray();
System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3, 0.4, 0.5]

double sum ()

Returns the sum of elements in this stream.

final var stream = DoubleStream.of(1.0, 2.0, 3.0, 4.0, 5.0);
System.out.println(stream.sum()); // 15.0
final var stream = DoubleStream.of(-0.1, -0.2, -0.3);
System.out.println(stream.sum()); // -0.6

DoubleSummaryStatistics summaryStatistics ()

Returns a DoubleSummaryStatistics describing various summary data about the elements of this stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3, 0.4, 0.5);
final var ret = stream.summaryStatistics();

// DoubleSummaryStatistics{count=5, sum=1.500000, min=0.100000, average=0.300000, max=0.500000}
System.out.println(ret);

default DoubleStream takeWhile (DoublePredicate predicate)

Returns, if this stream is ordered, a stream consisting of the longest prefix of elements taken from this stream that match the given predicate.

final var stream = DoubleStream.of(1.0, 2.0, 3.0, Double.NaN, 5.0);

final var predicate = new DoublePredicate() {
    @Override
    public boolean test(double value) {
        return !Double.isNaN(value);
    }
};

final var ret = stream.takeWhile(predicate).toArray();
System.out.println(Arrays.toString(ret)); // [1.0, 2.0, 3.0]
final var stream = DoubleStream.of(-1.0, -2.0, Double.NaN, -4.0, -5.0);

final var ret = stream.takeWhile(v -> !Double.isNaN(v)).toArray();
System.out.println(Arrays.toString(ret)); // [-1.0, -2.0]

double[] toArray ()

Returns an array containing the elements of this stream.

final var stream = DoubleStream.of(0.1, 0.2, 0.3);
final double[] ret = stream.toArray();

System.out.println(Arrays.toString(ret)); // [0.1, 0.2, 0.3]

Methods declared in BaseStream

close, isParallel, iterator, onClose, parallel, sequential, spliterator, unordered

Please see the link below.


Related posts

To top of page