public final class DoubleStream extends Object implements Closeable
double-valued elements supporting aggregate operations.Stream| Modifier and Type | Method and Description |
|---|---|
boolean |
allMatch(DoublePredicate predicate)
Tests whether all elements match the given predicate.
|
boolean |
anyMatch(DoublePredicate predicate)
Tests whether all elements match the given predicate.
|
OptionalDouble |
average()
Returns the average of elements in this stream.
|
Stream<Double> |
boxed()
Returns a
Stream consisting of the elements of this stream,
each boxed to an Double. |
void |
close()
Causes close handler to be invoked if it exists.
|
<R> R |
collect(Supplier<R> supplier,
ObjDoubleConsumer<R> accumulator)
Collects elements to
supplier provided container by applying the given accumulation function. |
static DoubleStream |
concat(DoubleStream a,
DoubleStream b)
Concatenates two streams.
|
long |
count()
Returns the count of elements in this stream.
|
<R> R |
custom(Function<DoubleStream,R> function)
Applies custom operator on stream.
|
DoubleStream |
distinct()
Returns a stream consisting of the distinct elements of this stream.
|
DoubleStream |
dropWhile(DoublePredicate predicate)
Drops elements while the predicate is true and returns the rest.
|
static DoubleStream |
empty()
Returns an empty stream.
|
DoubleStream |
filter(DoublePredicate predicate)
Returns
DoubleStream with elements that satisfy the given predicate. |
DoubleStream |
filterIndexed(IndexedDoublePredicate predicate)
Returns a
DoubleStream with elements that satisfy the given IndexedDoublePredicate. |
DoubleStream |
filterIndexed(int from,
int step,
IndexedDoublePredicate predicate)
Returns a
DoubleStream with elements that satisfy the given IndexedDoublePredicate. |
DoubleStream |
filterNot(DoublePredicate predicate)
Returns
DoubleStream with elements that does not satisfy the given predicate. |
OptionalDouble |
findFirst()
Returns the first element wrapped by
OptionalDouble class. |
OptionalDouble |
findLast()
Returns the last element wrapped by
OptionalDouble class. |
OptionalDouble |
findSingle()
Returns the single element wrapped by
OptionalDouble class. |
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.
|
void |
forEach(DoubleConsumer action)
Performs an action for each element of this stream.
|
void |
forEachIndexed(IndexedDoubleConsumer action)
Performs the given indexed action on each element.
|
void |
forEachIndexed(int from,
int step,
IndexedDoubleConsumer action)
Performs the given indexed action on each element.
|
static DoubleStream |
generate(DoubleSupplier s)
Creates a
DoubleStream by elements that generated by DoubleSupplier. |
static DoubleStream |
iterate(double seed,
DoublePredicate predicate,
DoubleUnaryOperator op)
Creates an
DoubleStream by iterative application DoubleUnaryOperator function
to an initial element seed, conditioned on satisfying the supplied predicate. |
static DoubleStream |
iterate(double seed,
DoubleUnaryOperator f)
Creates a
DoubleStream by iterative application DoubleUnaryOperator function
to an initial element seed. |
PrimitiveIterator.OfDouble |
iterator()
Returns internal
DoubleStream iterator. |
DoubleStream |
limit(long maxSize)
Returns a stream consisting of the elements of this stream, truncated
to be no longer than
maxSize in length. |
DoubleStream |
map(DoubleUnaryOperator mapper)
Returns an
DoubleStream consisting of the results of applying the given
function to the elements of this stream. |
DoubleStream |
mapIndexed(IndexedDoubleUnaryOperator mapper)
Returns a
DoubleStream with elements that obtained
by applying the given IndexedDoubleUnaryOperator. |
DoubleStream |
mapIndexed(int from,
int step,
IndexedDoubleUnaryOperator mapper)
Returns a
DoubleStream with elements that obtained
by applying the given IndexedDoubleUnaryOperator. |
IntStream |
mapToInt(DoubleToIntFunction mapper)
Returns an
IntStream consisting of the results of applying the given
function to the elements of this stream. |
LongStream |
mapToLong(DoubleToLongFunction mapper)
Returns an
LongStream consisting of the results of applying the given
function to the elements of this stream. |
<R> Stream<R> |
mapToObj(DoubleFunction<? extends R> mapper)
Returns a
Stream consisting of the results of applying the given
function to the elements of this stream. |
OptionalDouble |
max()
Returns an
OptionalDouble describing the maximum element of this
stream, or an empty optional if this stream is empty. |
OptionalDouble |
min()
Returns an
OptionalDouble describing the minimum element of this
stream, or an empty optional if this stream is empty. |
boolean |
noneMatch(DoublePredicate predicate)
Tests whether no elements match the given predicate.
|
static DoubleStream |
of(double... values)
Creates a
DoubleStream from the specified values. |
static DoubleStream |
of(double t)
Returns stream which contains single element passed as param
|
static DoubleStream |
of(PrimitiveIterator.OfDouble iterator)
Creates a
DoubleStream from PrimitiveIterator.OfDouble. |
DoubleStream |
onClose(Runnable closeHandler)
Adds close handler to the current stream.
|
DoubleStream |
peek(DoubleConsumer action)
Performs provided action on each element.
|
OptionalDouble |
reduce(DoubleBinaryOperator accumulator)
Performs a reduction on the elements of this stream, using an
associative accumulation function, and returns an
OptionalDouble
describing the reduced value, if any. |
double |
reduce(double identity,
DoubleBinaryOperator accumulator)
Performs a reduction on the elements of this stream, using the provided
identity value and an associative accumulation function, and returns the
reduced value.
|
DoubleStream |
sample(int stepWidth)
Samples the
DoubleStream by emitting every n-th element. |
DoubleStream |
scan(DoubleBinaryOperator accumulator)
Returns a
DoubleStream produced by iterative application of a accumulation function
to reduction value and next element of the current stream. |
DoubleStream |
scan(double identity,
DoubleBinaryOperator accumulator)
Returns a
DoubleStream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream. |
double |
single()
Returns the single element of stream.
|
DoubleStream |
skip(long n)
Skips first
n elements and returns DoubleStream with remaining elements. |
DoubleStream |
sorted()
Returns a stream consisting of the elements of this stream in sorted order.
|
DoubleStream |
sorted(Comparator<Double> comparator)
Returns a stream consisting of the elements of this stream
in sorted order as determinated by provided
Comparator. |
double |
sum()
Returns the sum of elements in this stream.
|
DoubleStream |
takeUntil(DoublePredicate stopPredicate)
Takes elements while the predicate returns
false. |
DoubleStream |
takeWhile(DoublePredicate predicate)
Takes elements while the predicate returns
true. |
double[] |
toArray()
Returns an array containing the elements of this stream.
|
public static DoubleStream empty()
public static DoubleStream of(PrimitiveIterator.OfDouble iterator)
DoubleStream from PrimitiveIterator.OfDouble.iterator - the iterator with elements to be passed to streamDoubleStreamNullPointerException - if iterator is nullpublic static DoubleStream of(double... values)
DoubleStream from the specified values.values - the elements of the new streamNullPointerException - if values is nullpublic static DoubleStream of(double t)
t - element of the streampublic static DoubleStream generate(DoubleSupplier s)
DoubleStream by elements that generated by DoubleSupplier.s - the DoubleSupplier for generated elementsDoubleStreamNullPointerException - if s is nullpublic static DoubleStream iterate(double seed, DoubleUnaryOperator f)
DoubleStream by iterative application DoubleUnaryOperator function
to an initial element seed. Produces DoubleStream consisting of
seed, f(seed), f(f(seed)), etc.
The first element (position 0) in the DoubleStream will be
the provided seed. For n > 0, the element at position
n, will be the result of applying the function f to the
element at position n - 1.
Example:
seed: 1 f: (a) -> a + 5 result: [1, 6, 11, 16, ...]
seed - the initial elementf - a function to be applied to the previous element to produce a new elementDoubleStreamNullPointerException - if f is nullpublic static DoubleStream iterate(double seed, DoublePredicate predicate, DoubleUnaryOperator op)
DoubleStream by iterative application DoubleUnaryOperator function
to an initial element seed, conditioned on satisfying the supplied predicate.
Example:
seed: 0.0 predicate: (a) -> a < 0.2 f: (a) -> a + 0.05 result: [0.0, 0.05, 0.1, 0.15]
seed - the initial valuepredicate - a predicate to determine when the stream must terminateop - operator to produce new element by previous oneNullPointerException - if op is nullpublic static DoubleStream concat(DoubleStream a, DoubleStream b)
Example:
stream a: [1, 2, 3, 4] stream b: [5, 6] result: [1, 2, 3, 4, 5, 6]
a - the first streamb - the second streamNullPointerException - if a or b is nullpublic PrimitiveIterator.OfDouble iterator()
DoubleStream iterator.DoubleStream iterator.public <R> R custom(Function<DoubleStream,R> function)
DoubleStream for intermediate operations,
or any value for terminal operation.
Operator examples:
// Intermediate operator
public class Zip implements Function<DoubleStream, DoubleStream> {
private final DoubleStream secondStream;
private final DoubleBinaryOperator combiner;
public Zip(DoubleStream secondStream, DoubleBinaryOperator combiner) {
this.secondStream = secondStream;
this.combiner = combiner;
}
@Override
public DoubleStream apply(DoubleStream firstStream) {
final PrimitiveIterator.OfDouble it1 = firstStream.iterator();
final PrimitiveIterator.OfDouble it2 = secondStream.iterator();
return DoubleStream.of(new PrimitiveIterator.OfDouble() {
@Override
public boolean hasNext() {
return it1.hasNext() && it2.hasNext();
}
@Override
public double nextDouble() {
return combiner.applyAsDouble(it1.nextDouble(), it2.nextDouble());
}
});
}
}
// Intermediate operator based on existing stream operators
public class SkipAndLimit implements UnaryOperator<DoubleStream> {
private final int skip, limit;
public SkipAndLimit(int skip, int limit) {
this.skip = skip;
this.limit = limit;
}
@Override
public DoubleStream apply(DoubleStream stream) {
return stream.skip(skip).limit(limit);
}
}
// Terminal operator
public class DoubleSummaryStatistics implements Function<DoubleStream, double[]> {
@Override
public double[] apply(DoubleStream stream) {
long count = 0;
double sum = 0;
final PrimitiveIterator.OfDouble it = stream.iterator();
while (it.hasNext()) {
count++;
sum += it.nextDouble();
}
double average = (count == 0) ? 0 : (sum / (double) count);
return new double[] {count, sum, average};
}
}
R - the type of the resultfunction - a transforming functionNullPointerException - if function is nullStream.custom(com.annimon.stream.function.Function)public Stream<Double> boxed()
Stream consisting of the elements of this stream,
each boxed to an Double.
This is an lazy intermediate operation.
Stream consistent of the elements of this stream,
each boxed to an Doublepublic DoubleStream filter(DoublePredicate predicate)
DoubleStream with elements that satisfy the given predicate.
This is an intermediate operation.
Example:
predicate: (a) -> a > 2 stream: [1, 2, 3, 4, -8, 0, 11] result: [3, 4, 11]
predicate - the predicate used to filter elementspublic DoubleStream filterIndexed(IndexedDoublePredicate predicate)
DoubleStream with elements that satisfy the given IndexedDoublePredicate.
This is an intermediate operation.
Example:
predicate: (index, value) -> (index + value) > 6 stream: [1, 2, 3, 4, 0, 11] index: [0, 1, 2, 3, 4, 5] sum: [1, 3, 5, 7, 4, 16] filter: [ 7, 16] result: [4, 11]
predicate - the IndexedDoublePredicate used to filter elementspublic DoubleStream filterIndexed(int from, int step, IndexedDoublePredicate predicate)
DoubleStream with elements that satisfy the given IndexedDoublePredicate.
This is an intermediate operation.
Example:
from: 4 step: 3 predicate: (index, value) -> (index + value) > 15 stream: [1, 2, 3, 4, 0, 11] index: [4, 7, 10, 13, 16, 19] sum: [5, 9, 13, 17, 16, 30] filter: [ 17, 16, 30] result: [4, 0, 11]
from - the initial value of the index (inclusive)step - the step of the indexpredicate - the IndexedDoublePredicate used to filter elementspublic DoubleStream filterNot(DoublePredicate predicate)
DoubleStream with elements that does not satisfy the given predicate.
This is an intermediate operation.
predicate - the predicate used to filter elementspublic DoubleStream map(DoubleUnaryOperator mapper)
DoubleStream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
Example:
mapper: (a) -> a + 5 stream: [1, 2, 3, 4] result: [6, 7, 8, 9]
mapper - the mapper function used to apply to each elementStream.map(com.annimon.stream.function.Function)public DoubleStream mapIndexed(IndexedDoubleUnaryOperator mapper)
DoubleStream with elements that obtained
by applying the given IndexedDoubleUnaryOperator.
This is an intermediate operation.
Example:
mapper: (index, value) -> (index * value) stream: [1, 2, 3, 4] index: [0, 1, 2, 3] result: [0, 2, 6, 12]
mapper - the mapper function used to apply to each elementpublic DoubleStream mapIndexed(int from, int step, IndexedDoubleUnaryOperator mapper)
DoubleStream with elements that obtained
by applying the given IndexedDoubleUnaryOperator.
This is an intermediate operation.
Example:
from: -2 step: 2 mapper: (index, value) -> (index * value) stream: [ 1, 2, 3, 4] index: [-2, 0, 2, 4] result: [-2, 0, 6, 16]
from - the initial value of the index (inclusive)step - the step of the indexmapper - the mapper function used to apply to each elementpublic <R> Stream<R> mapToObj(DoubleFunction<? extends R> mapper)
Stream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
R - the type resultmapper - the mapper function used to apply to each elementStreampublic IntStream mapToInt(DoubleToIntFunction mapper)
IntStream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementIntStreampublic LongStream mapToLong(DoubleToLongFunction mapper)
LongStream consisting of the results of applying the given
function to the elements of this stream.
This is an intermediate operation.
mapper - the mapper function used to apply to each elementLongStreampublic DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)
This is an intermediate operation.
Example:
mapper: (a) -> [a, a + 5] stream: [1, 2, 3, 4] result: [1, 6, 2, 7, 3, 8, 4, 9]
mapper - the mapper function used to apply to each elementStream.flatMap(com.annimon.stream.function.Function)public DoubleStream distinct()
This is a stateful intermediate operation.
Example:
stream: [1, 4, 2, 3, 3, 4, 1] result: [1, 4, 2, 3]
public DoubleStream sorted()
This is a stateful intermediate operation.
Example:
stream: [3, 4, 1, 2] result: [1, 2, 3, 4]
public DoubleStream sorted(Comparator<Double> comparator)
Comparator.
This is a stateful intermediate operation.
Example:
comparator: (a, b) -> -a.compareTo(b) stream: [1, 2, 3, 4] result: [4, 3, 2, 1]
comparator - the Comparator to compare elementsDoubleStreampublic DoubleStream sample(int stepWidth)
DoubleStream by emitting every n-th element.
This is an intermediate operation.
Example:
stepWidth: 3 stream: [1, 2, 3, 4, 5, 6, 7, 8] result: [1, 4, 7]
stepWidth - step widthDoubleStreamIllegalArgumentException - if stepWidth is zero or negativeStream.sample(int)public DoubleStream peek(DoubleConsumer action)
This is an intermediate operation.
action - the action to be performed on each elementpublic DoubleStream scan(DoubleBinaryOperator accumulator)
DoubleStream produced by iterative application of a accumulation function
to reduction value and next element of the current stream.
Produces a DoubleStream consisting of value1, acc(value1, value2),
acc(acc(value1, value2), value3), etc.
This is an intermediate operation.
Example:
accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [1, 3, 6, 10, 15]
accumulator - the accumulation functionNullPointerException - if accumulator is nullpublic DoubleStream scan(double identity, DoubleBinaryOperator accumulator)
DoubleStream produced by iterative application of a accumulation function
to an initial element identity and next element of the current stream.
Produces a DoubleStream consisting of identity, acc(identity, value1),
acc(acc(identity, value1), value2), etc.
This is an intermediate operation.
Example:
identity: 0 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: [0, 1, 3, 6, 10, 15]
identity - the initial valueaccumulator - the accumulation functionNullPointerException - if accumulator is nullpublic DoubleStream takeWhile(DoublePredicate predicate)
true.
This is an intermediate operation.
Example:
predicate: (a) -> a < 3 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [1, 2]
predicate - the predicate used to take elementsDoubleStreampublic DoubleStream takeUntil(DoublePredicate stopPredicate)
false.
Once predicate condition is satisfied by an element, the stream
finishes with this element.
This is an intermediate operation.
Example:
stopPredicate: (a) -> a > 2 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [1, 2, 3]
stopPredicate - the predicate used to take elementsDoubleStreampublic DoubleStream dropWhile(DoublePredicate predicate)
This is an intermediate operation.
Example:
predicate: (a) -> a < 3 stream: [1, 2, 3, 4, 1, 2, 3, 4] result: [3, 4, 1, 2, 3, 4]
predicate - the predicate used to drop elementsDoubleStreampublic DoubleStream limit(long maxSize)
maxSize in length.
This is a short-circuiting stateful intermediate operation.
Example:
maxSize: 3 stream: [1, 2, 3, 4, 5] result: [1, 2, 3] maxSize: 10 stream: [1, 2] result: [1, 2]
maxSize - the number of elements the stream should be limited toIllegalArgumentException - if maxSize is negativepublic DoubleStream skip(long n)
n elements and returns DoubleStream with remaining elements.
If this stream contains fewer than n elements, then an
empty stream will be returned.
This is a stateful intermediate operation.
Example:
n: 3 stream: [1, 2, 3, 4, 5] result: [4, 5] n: 10 stream: [1, 2] result: []
n - the number of elements to skipIllegalArgumentException - if n is negativepublic void forEach(DoubleConsumer action)
This is a terminal operation.
action - the action to be performed on each elementpublic void forEachIndexed(IndexedDoubleConsumer action)
This is a terminal operation.
action - the action to be performed on each elementpublic void forEachIndexed(int from,
int step,
IndexedDoubleConsumer action)
This is a terminal operation.
from - the initial value of the index (inclusive)step - the step of the indexaction - the action to be performed on each elementpublic double reduce(double identity,
DoubleBinaryOperator accumulator)
The identity value must be an identity for the accumulator
function. This means that for all x,
accumulator.apply(identity, x) is equal to x.
The accumulator function must be an associative function.
This is a terminal operation.
Example:
identity: 0 accumulator: (a, b) -> a + b stream: [1, 2, 3, 4, 5] result: 15
public OptionalDouble reduce(DoubleBinaryOperator accumulator)
OptionalDouble
describing the reduced value, if any.
The accumulator function must be an associative function.
This is a terminal operation.
accumulator - the accumulation functionreduce(com.annimon.stream.function.DoubleBinaryOperator)public double[] toArray()
This is a terminal operation.
public <R> R collect(Supplier<R> supplier, ObjDoubleConsumer<R> accumulator)
supplier provided container by applying the given accumulation function.
This is a terminal operation.
R - the type of the resultsupplier - the supplier function that provides containeraccumulator - the accumulation functionStream.collect(com.annimon.stream.function.Supplier, com.annimon.stream.function.BiConsumer)public double sum()
public OptionalDouble min()
OptionalDouble describing the minimum element of this
stream, or an empty optional if this stream is empty.
This is a terminal operation.
public OptionalDouble max()
OptionalDouble describing the maximum element of this
stream, or an empty optional if this stream is empty.
This is a terminal operation.
public long count()
This is a terminal operation.
public OptionalDouble average()
This is a terminal operation.
public boolean anyMatch(DoublePredicate predicate)
false is returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: true predicate: (a) -> a == 5 stream: [5, 5, 5] result: true
predicate - the predicate used to match elementstrue if any elements of the stream match the provided
predicate, otherwise falsepublic boolean allMatch(DoublePredicate predicate)
true is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: false predicate: (a) -> a == 5 stream: [5, 5, 5] result: true
predicate - the predicate used to match elementstrue if either all elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic boolean noneMatch(DoublePredicate predicate)
true is
returned and the predicate is not evaluated.
This is a short-circuiting terminal operation.
Example:
predicate: (a) -> a == 5 stream: [1, 2, 3, 4, 5] result: false predicate: (a) -> a == 5 stream: [1, 2, 3] result: true
predicate - the predicate used to match elementstrue if either no elements of the stream match the
provided predicate or the stream is empty, otherwise falsepublic OptionalDouble findFirst()
OptionalDouble class.
If stream is empty, returns OptionalDouble.empty().
This is a short-circuiting terminal operation.
OptionalDouble with first element
or OptionalDouble.empty() if stream is emptypublic OptionalDouble findLast()
OptionalDouble class.
If stream is empty, returns OptionalDouble.empty().
This is a short-circuiting terminal operation.
OptionalDouble with the last element
or OptionalDouble.empty() if the stream is emptypublic double single()
NoSuchElementException.
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: NoSuchElementException stream: [1] result: 1 stream: [1, 2, 3] result: IllegalStateException
NoSuchElementException - if stream is emptyIllegalStateException - if stream contains more than one elementpublic OptionalDouble findSingle()
OptionalDouble class.
If stream is empty, returns OptionalDouble.empty().
If stream contains more than one element, throws IllegalStateException.
This is a short-circuiting terminal operation.
Example:
stream: [] result: OptionalDouble.empty() stream: [1] result: OptionalDouble.of(1) stream: [1, 2, 3] result: IllegalStateException
OptionalDouble with single element
or OptionalDouble.empty() if stream is emptyIllegalStateException - if stream contains more than one elementpublic DoubleStream onClose(Runnable closeHandler)
This is an intermediate operation.
closeHandler - an action to execute when the stream is closedpublic void close()
close in interface Closeableclose in interface AutoCloseableCopyright © 2018. All rights reserved.