Most visited

Recently visited

Added in API level 24

DoubleStream

public interface DoubleStream
implements BaseStream<DoubleDoubleStream>

java.util.stream.DoubleStream


支持顺序和并行聚合操作的一系列原始双值元素。 这是double原始专业化Stream

以下示例说明了一个使用 StreamDoubleStream的聚合操作,计算红色小部件的权重总和:

double sum = widgets.stream()
                         .filter(w -> w.getColor() == RED)
                         .mapToDouble(w -> w.getWeight())
                         .sum();
 
See the class documentation for Stream and the package documentation for java.util.stream for additional specification of streams, stream operations, stream pipelines, and parallelism.

也可以看看:

Summary

Nested classes

interface DoubleStream.Builder

DoubleStream可变建设者。

Public methods

abstract boolean allMatch(DoublePredicate predicate)

返回此流的所有元素是否与提供的谓词匹配。

abstract boolean anyMatch(DoublePredicate predicate)

返回此流的任何元素是否与提供的谓词匹配。

abstract OptionalDouble average()

返回描述该流的元素的算术平均值的 OptionalDouble ,或者如果此流为空,则返回一个空的可选项。

abstract Stream<Double> boxed()

返回由此流的元素组成的 Stream ,装箱到 Double

static DoubleStream.Builder builder()

返回 DoubleStream的构建器。

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

对此流的元素执行 mutable reduction操作。

static DoubleStream concat(DoubleStream a, DoubleStream b)

创建一个延迟连接的流,其元素是第一个流的所有元素,后跟第二个流的所有元素。

abstract long count()

返回此流中元素的数量。

abstract DoubleStream distinct()

返回由此流的不同元素组成的流。

static DoubleStream empty()

返回一个空的顺序 DoubleStream

abstract DoubleStream filter(DoublePredicate predicate)

返回包含此流中与给定谓词相匹配的元素的流。

abstract OptionalDouble findAny()

返回描述流的某个元素的 OptionalDouble ,或者如果流为空,则返回空 OptionalDouble

abstract OptionalDouble findFirst()

返回 OptionalDouble描述此流的第一个元素,或空 OptionalDouble如果流是空的。

abstract DoubleStream flatMap(DoubleFunction<? extends DoubleStream> mapper)

返回一个流,该流包含将此流的每个元素替换为通过将所提供的映射函数应用于每个元素而生成的映射流的内容的结果。

abstract void forEach(DoubleConsumer action)

为此流的每个元素执行操作。

abstract void forEachOrdered(DoubleConsumer action)

对此流的每个元素执行一个操作,保证每个元素都按遇到顺序处理,以获得具有已定义的遇到顺序的流。

static DoubleStream generate(DoubleSupplier s)

返回无限顺序无序流,其中每个元素由提供的 DoubleSupplier生成。

static DoubleStream iterate(double seed, DoubleUnaryOperator f)

返回有序无限连续 DoubleStream由函数的迭代应用产生 f到初始元素 seed ,产生 Streamseedf(seed)f(f(seed))

abstract PrimitiveIterator.OfDouble iterator()

返回此流的元素的迭代器。

abstract DoubleStream limit(long maxSize)

返回由该流的元素组成的流,截断长度不超过 maxSize

abstract DoubleStream map(DoubleUnaryOperator mapper)

返回由将给定函数应用于此流的元素的结果组成的流。

abstract IntStream mapToInt(DoubleToIntFunction mapper)

返回一个 IntStream其中包含将给定函数应用于此流的元素的结果。

abstract LongStream mapToLong(DoubleToLongFunction mapper)

返回一个 LongStream其中包含将给定函数应用于此流的元素的结果。

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

返回一个对象值 Stream ,该值包含将给定函数应用于此流的元素的结果。

abstract OptionalDouble max()

返回描述此流最大元素的 OptionalDouble ,如果此流为空,则返回空的OptionalDouble。

abstract OptionalDouble min()

返回描述此流的最小元素的 OptionalDouble ,如果此流为空,则返回空的OptionalDouble。

abstract boolean noneMatch(DoublePredicate predicate)

返回此流的元素是否与提供的谓词匹配。

static DoubleStream of(double... values)

返回顺序排列的流,其元素是指定的值。

static DoubleStream of(double t)

返回包含单个元素的顺序 DoubleStream

abstract DoubleStream parallel()

返回平行的等效流。

abstract DoubleStream peek(DoubleConsumer action)

返回由该流的元素组成的流,并在元素从结果流中消耗时另外对每个元素执行提供的操作。

abstract OptionalDouble reduce(DoubleBinaryOperator op)

使用 associative累加函数对此流的元素执行 reduction ,并返回描述减小值的 OptionalDouble (如果有)。

abstract double reduce(double identity, DoubleBinaryOperator op)

对此流的元素执行 reduction ,使用提供的标识值和 associative累加函数,并返回缩小的值。

abstract DoubleStream sequential()

返回顺序的等效流。

abstract DoubleStream skip(long n)

在丢弃流的第一个 n元素之后,返回由该流的其余元素组成的流。

abstract DoubleStream sorted()

以排序顺序返回由此流的元素组成的流。

abstract Spliterator.OfDouble spliterator()

返回此流的元素的分割器。

abstract double sum()

返回此流中元素的总和。

abstract DoubleSummaryStatistics summaryStatistics()

返回描述有关此流元素的各种摘要数据的 DoubleSummaryStatistics

abstract double[] toArray()

返回包含此流的元素的数组。

Inherited methods

From interface java.util.stream.BaseStream
From interface java.lang.AutoCloseable

Public methods

allMatch

Added in API level 24
boolean allMatch (DoublePredicate predicate)

返回此流的所有元素是否与提供的谓词匹配。 如果不需要确定结果,则不能评估所有元素的谓词。 如果该流为空,则返回true ,并且不评估谓词。

这是一个 short-circuiting terminal operation

API Note:
  • This method evaluates the universal quantification of the predicate over the elements of the stream (for all x P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true (regardless of P(x)).
Parameters
predicate DoublePredicate: a non-interfering, stateless predicate to apply to elements of this stream
Returns
boolean true if either all elements of the stream match the provided predicate or the stream is empty, otherwise false

anyMatch

Added in API level 24
boolean anyMatch (DoublePredicate predicate)

返回此流的任何元素是否与提供的谓词匹配。 如果不需要确定结果,则不能评估所有元素的谓词。 如果流为空,则返回false ,并且不评估谓词。

这是一个 short-circuiting terminal operation

API Note:
  • This method evaluates the existential quantification of the predicate over the elements of the stream (for some x P(x)).
Parameters
predicate DoublePredicate: a non-interfering, stateless predicate to apply to elements of this stream
Returns
boolean true if any elements of the stream match the provided predicate, otherwise false

average

Added in API level 24
OptionalDouble average ()

返回描述该流的元素的算术平均值的OptionalDouble ,或者如果此流为空,则返回一个空的可选元素。 如果任何记录的值是NaN或总和在任何点NaN那么平均值将是NaN。

返回的平均值取决于记录值的顺序。 该方法可以使用补偿总和或其他技术来实现,以减少用于计算平均值的numerical sum的误差界限。

平均值是 reduction的特例

这是一个 terminal operation

API Note:
  • Elements sorted by increasing absolute magnitude tend to yield more accurate results.
Returns
OptionalDouble an OptionalDouble containing the average element of this stream, or an empty optional if the stream is empty

boxed

Added in API level 24
Stream<Double> boxed ()

返回由此流的元素组成的 Stream ,装箱到 Double

这是一个 intermediate operation

Returns
Stream<Double> a Stream consistent of the elements of this stream, each boxed to a Double

builder

Added in API level 24
DoubleStream.Builder builder ()

返回 DoubleStream的构建器。

Returns
DoubleStream.Builder a stream builder

collect

Added in API level 24
R collect (Supplier<R> supplier, 
                ObjDoubleConsumer<R> accumulator, 
                BiConsumer<R, R> combiner)

对此流的元素执行mutable reduction操作。 可变的约简是减少的值是一个可变的结果容器,例如ArrayList ,并且通过更新结果的状态而不是通过替换结果来合并元素。 这产生的结果等同于:

R result = supplier.get();
     for (double element : this stream)
         accumulator.accept(result, element);
     return result;
 

reduce(double, DoubleBinaryOperator)一样, collect操作可以并行化而不需要额外的同步。

这是一个 terminal operation

Parameters
supplier Supplier: a function that creates a new result container. For a parallel execution, this function may be called multiple times and must return a fresh value each time.
accumulator ObjDoubleConsumer: an associative, non-interfering, stateless function for incorporating an additional element into a result
combiner BiConsumer: an associative, non-interfering, stateless function for combining two values, which must be compatible with the accumulator function
Returns
R the result of the reduction

也可以看看:

concat

Added in API level 24
DoubleStream concat (DoubleStream a, 
                DoubleStream b)

创建一个延迟连接的流,其元素是第一个流的所有元素,后跟第二个流的所有元素。 如果两个输入流都是有序的,则生成的流是有序的,如果任意一个输入流是并行的,则生成流。 当结果流关闭时,调用两个输入流的关闭处理程序。

Implementation Note:
  • Use caution when constructing streams from repeated concatenation. Accessing an element of a deeply concatenated stream can result in deep call chains, or even StackOverflowException.
Parameters
a DoubleStream: the first stream
b DoubleStream: the second stream
Returns
DoubleStream the concatenation of the two input streams

count

Added in API level 24
long count ()

返回此流中元素的数量。 这是reduction的特例 ,相当于:

return mapToLong(e -> 1L).sum();
 

这是一个 terminal operation

Returns
long the count of elements in this stream

distinct

Added in API level 24
DoubleStream distinct ()

返回由此流的不同元素组成的流。 根据compare(double, double)比较元素的相等compare(double, double)

这是一个 stateful intermediate operation

Returns
DoubleStream the result stream

empty

Added in API level 24
DoubleStream empty ()

返回一个空的顺序 DoubleStream

Returns
DoubleStream an empty sequential stream

filter

Added in API level 24
DoubleStream filter (DoublePredicate predicate)

返回包含此流中与给定谓词相匹配的元素的流。

这是一个 intermediate operation

Parameters
predicate DoublePredicate: a non-interfering, stateless predicate to apply to each element to determine if it should be included
Returns
DoubleStream the new stream

findAny

Added in API level 24
OptionalDouble findAny ()

返回描述流的某个元素的 OptionalDouble ,或者如果流为空,则返回空 OptionalDouble

这是一个 short-circuiting terminal operation

这种操作的行为显然是不确定的; 它可以自由选择流中的任何元素。 这是为了在并行操作中实现最高性能; 成本是在同一个源上的多个调用可能不会返回相同的结果。 (如果需要稳定的结果, findFirst()改为使用findFirst()

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

也可以看看:

findFirst

Added in API level 24
OptionalDouble findFirst ()

返回OptionalDouble描述此流的第一个元素,或空OptionalDouble如果流是空的。 如果该流没有遇到命令,则可以返回任何元素。

这是一个 short-circuiting terminal operation

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

flatMap

Added in API level 24
DoubleStream flatMap (DoubleFunction<? extends DoubleStream> mapper)

返回一个流,该流包含将此流的每个元素替换为通过将所提供的映射函数应用于每个元素而生成的映射流的内容的结果。 将其内容放入此流后,每个映射流都是closed (如果映射流为null ,则使用空流。)

这是一个 intermediate operation

Parameters
mapper DoubleFunction: a non-interfering, stateless function to apply to each element which produces a DoubleStream of new values
Returns
DoubleStream the new stream

也可以看看:

forEach

Added in API level 24
void forEach (DoubleConsumer action)

为此流的每个元素执行操作。

这是一个 terminal operation

对于并行流管道,此操作并不保证尊重流的相遇顺序,因为这样做会牺牲并行的利益。 对于任何给定的元素,该动作可以在任何时间和库中选择的任何线程中执行。 如果操作访问共享状态,它负责提供所需的同步。

Parameters
action DoubleConsumer: a non-interfering action to perform on the elements

forEachOrdered

Added in API level 24
void forEachOrdered (DoubleConsumer action)

对此流的每个元素执行一个操作,保证每个元素都按遇到顺序处理,以获得具有已定义的遇到顺序的流。

这是一个 terminal operation

Parameters
action DoubleConsumer: a non-interfering action to perform on the elements

也可以看看:

generate

Added in API level 24
DoubleStream generate (DoubleSupplier s)

返回无限顺序无序流,其中每个元素由提供的DoubleSupplier生成。 这适用于生成恒定流,随机元素流等。

Parameters
s DoubleSupplier: the DoubleSupplier for generated elements
Returns
DoubleStream a new infinite sequential unordered DoubleStream

iterate

Added in API level 24
DoubleStream iterate (double seed, 
                DoubleUnaryOperator f)

返回有序无限连续 DoubleStream由函数的迭代应用产生 f到初始元素 seed ,产生 Streamseedf(seed)f(f(seed))

第一元件(位置0在) DoubleStream将是提供seed 对于n > 0 ,位置n处的元素将是将函数f应用于位置n - 1处的元素的n - 1

Parameters
seed double: the initial element
f DoubleUnaryOperator: a function to be applied to to the previous element to produce a new element
Returns
DoubleStream a new sequential DoubleStream

iterator

Added in API level 24
PrimitiveIterator.OfDouble iterator ()

返回此流的元素的迭代器。

这是一个 terminal operation

Returns
PrimitiveIterator.OfDouble the element iterator for this stream

limit

Added in API level 24
DoubleStream limit (long maxSize)

返回由该流的元素组成的流,截断长度不超过 maxSize

这是一个 short-circuiting stateful intermediate operation

API Note:
  • While limit() is generally a cheap operation on sequential stream pipelines, it can be quite expensive on ordered parallel pipelines, especially for large values of maxSize, since limit(n) is constrained to return not just any n elements, but the first n elements in the encounter order. Using an unordered stream source (such as generate(DoubleSupplier)) or removing the ordering constraint with unordered() may result in significant speedups of limit() in parallel pipelines, if the semantics of your situation permit. If consistency with encounter order is required, and you are experiencing poor performance or memory utilization with limit() in parallel pipelines, switching to sequential execution with sequential() may improve performance.
Parameters
maxSize long: the number of elements the stream should be limited to
Returns
DoubleStream the new stream
Throws
IllegalArgumentException if maxSize is negative

map

Added in API level 24
DoubleStream map (DoubleUnaryOperator mapper)

返回由将给定函数应用于此流的元素的结果组成的流。

这是一个 intermediate operation

Parameters
mapper DoubleUnaryOperator: a non-interfering, stateless function to apply to each element
Returns
DoubleStream the new stream

mapToInt

Added in API level 24
IntStream mapToInt (DoubleToIntFunction mapper)

返回由将给定函数应用于此流的元素的结果组成的 IntStream

这是一个 intermediate operation

Parameters
mapper DoubleToIntFunction: a non-interfering, stateless function to apply to each element
Returns
IntStream the new stream

mapToLong

Added in API level 24
LongStream mapToLong (DoubleToLongFunction mapper)

返回一个 LongStream其中包含将给定函数应用于此流的元素的结果。

这是一个 intermediate operation

Parameters
mapper DoubleToLongFunction: a non-interfering, stateless function to apply to each element
Returns
LongStream the new stream

mapToObj

Added in API level 24
Stream<U> mapToObj (DoubleFunction<? extends U> mapper)

返回一个对象值 Stream ,该值包含将给定函数应用于此流的元素的结果。

这是一个 intermediate operation

Parameters
mapper DoubleFunction: a non-interfering, stateless function to apply to each element
Returns
Stream<U> the new stream

max

Added in API level 24
OptionalDouble max ()

返回描述此流最大元素的OptionalDouble ,如果此流为空,则返回空的OptionalDouble。 如果任何流元素是NaN,则最大元素将是Double.NaN 与数值比较运算符不同,此方法将负零视为严格小于正零。 这是reduction的特例 ,相当于:

return reduce(Double::max);
 

这是一个 terminal operation

Returns
OptionalDouble an OptionalDouble containing the maximum element of this stream, or an empty optional if the stream is empty

min

Added in API level 24
OptionalDouble min ()

返回描述此流最小元素的OptionalDouble ,如果此流为空,则返回空的OptionalDouble。 如果任何流元素是NaN,最小元素将是Double.NaN 与数值比较运算符不同,此方法将负零视为严格小于正零。 这是reduction的特例 ,相当于:

return reduce(Double::min);
 

这是一个 terminal operation

Returns
OptionalDouble an OptionalDouble containing the minimum element of this stream, or an empty optional if the stream is empty

noneMatch

Added in API level 24
boolean noneMatch (DoublePredicate predicate)

返回此流的元素是否与提供的谓词匹配。 如果不需要确定结果,则不能评估所有元素的谓词。 如果流为空,则返回true ,并且不评估谓词。

这是一个 short-circuiting terminal operation

API Note:
  • This method evaluates the universal quantification of the negated predicate over the elements of the stream (for all x ~P(x)). If the stream is empty, the quantification is said to be vacuously satisfied and is always true, regardless of P(x).
Parameters
predicate DoublePredicate: a non-interfering, stateless predicate to apply to elements of this stream
Returns
boolean true if either no elements of the stream match the provided predicate or the stream is empty, otherwise false

of

Added in API level 24
DoubleStream of (double... values)

返回顺序排列的流,其元素是指定的值。

Parameters
values double: the elements of the new stream
Returns
DoubleStream the new stream

of

Added in API level 24
DoubleStream of (double t)

返回包含单个元素的顺序 DoubleStream

Parameters
t double: the single element
Returns
DoubleStream a singleton sequential stream

parallel

Added in API level 24
DoubleStream parallel ()

返回平行的等效流。 可能会返回自己,因为流已经是平行的,或者因为基础流状态被修改为平行。

这是一个 intermediate operation

Returns
DoubleStream a parallel stream

peek

Added in API level 24
DoubleStream peek (DoubleConsumer action)

返回由该流的元素组成的流,并在元素从结果流中消耗时另外对每个元素执行提供的操作。

这是一个 intermediate operation

对于并行流管线,可以随时调用该操作,并且可以在任何线程中通过上游操作使该元素可用。 如果该操作修改共享状态,则它负责提供所需的同步。

API Note:
  • This method exists mainly to support debugging, where you want to see the elements as they flow past a certain point in a pipeline:
    DoubleStream.of(1, 2, 3, 4)
             .filter(e -> e > 2)
             .peek(e -> System.out.println("Filtered value: " + e))
             .map(e -> e * e)
             .peek(e -> System.out.println("Mapped value: " + e))
             .sum();
     
Parameters
action DoubleConsumer: a non-interfering action to perform on the elements as they are consumed from the stream
Returns
DoubleStream the new stream

reduce

Added in API level 24
OptionalDouble reduce (DoubleBinaryOperator op)

使用associative累加函数对此流的元素执行reduction ,并返回OptionalDouble描述减小的值(如果有)。 这相当于:

boolean foundAny = false;
     double result = null;
     for (double element : this stream) {
         if (!foundAny) {
             foundAny = true;
             result = element;
         }
         else
             result = accumulator.applyAsDouble(result, element);
     }
     return foundAny ? OptionalDouble.of(result) : OptionalDouble.empty();
 
but is not constrained to execute sequentially.

accumulator函数必须是 associative函数。

这是一个 terminal operation

Parameters
op DoubleBinaryOperator: an associative, non-interfering, stateless function for combining two values
Returns
OptionalDouble the result of the reduction

也可以看看:

reduce

Added in API level 24
double reduce (double identity, 
                DoubleBinaryOperator op)

使用提供的标识值和associative累积函数对此流的元素执行reduction ,并返回缩小的值。 这相当于:

double result = identity;
     for (double element : this stream)
         result = accumulator.applyAsDouble(result, element)
     return result;
 
but is not constrained to execute sequentially.

identity值必须是累加器函数的标识。 这意味着,对于所有xaccumulator.apply(identity, x)等于x accumulator函数必须是associative函数。

这是一个 terminal operation

API Note:
  • Sum, min, max, and average are all special cases of reduction. Summing a stream of numbers can be expressed as:
    double sum = numbers.reduce(0, (a, b) -> a+b);
     
    or more compactly:
    double sum = numbers.reduce(0, Double::sum);
     

    虽然与简单地在循环中改变运行总数相比,这看起来可能是一种更为迂回的方式来执行聚合,但减少操作可以更加平滑地进行并行处理,而不需要额外的同步并大大降低了数据竞争的风险。

Parameters
identity double: the identity value for the accumulating function
op DoubleBinaryOperator: an associative, non-interfering, stateless function for combining two values
Returns
double the result of the reduction

也可以看看:

sequential

Added in API level 24
DoubleStream sequential ()

返回顺序的等效流。 可能会返回自己,因为流已经是顺序的,或者因为基础流状态被修改为顺序的。

这是一个 intermediate operation

Returns
DoubleStream a sequential stream

skip

Added in API level 24
DoubleStream skip (long n)

在丢弃流的第一个n元素之后,返回由该流的其余元素组成的流。 如果此流包含的元素少于n则会返回空流。

这是一个 stateful intermediate operation

API Note:
  • While skip() is generally a cheap operation on sequential stream pipelines, it can be quite expensive on ordered parallel pipelines, especially for large values of n, since skip(n) is constrained to skip not just any n elements, but the first n elements in the encounter order. Using an unordered stream source (such as generate(DoubleSupplier)) or removing the ordering constraint with unordered() may result in significant speedups of skip() in parallel pipelines, if the semantics of your situation permit. If consistency with encounter order is required, and you are experiencing poor performance or memory utilization with skip() in parallel pipelines, switching to sequential execution with sequential() may improve performance.
Parameters
n long: the number of leading elements to skip
Returns
DoubleStream the new stream
Throws
IllegalArgumentException if n is negative

sorted

Added in API level 24
DoubleStream sorted ()

以排序顺序返回由此流的元素组成的流。 根据compare(double, double)比较元素的相等compare(double, double)

这是一个 stateful intermediate operation

Returns
DoubleStream the result stream

spliterator

Added in API level 24
Spliterator.OfDouble spliterator ()

返回此流的元素的分割器。

这是一个 terminal operation

Returns
Spliterator.OfDouble the element spliterator for this stream

sum

Added in API level 24
double sum ()

返回此流中元素的总和。 总结是reduction的特例 如果浮点求和精确,则此方法将等同于:

return reduce(0, Double::sum);
 
However, since floating-point summation is not exact, the above code is not necessarily equivalent to the summation computation done by this method.

如果任何流元素是NaN或总和在任何一个点上是NaN,那么总和将是NaN。 浮点和的值既是输入值又是加法操作的顺序。 这种方法的附加操作的顺序是故意没有定义的,以允许实现的灵活性来提高计算结果的速度和准确性。 特别地,该方法可以使用补偿求和或其他技术来减少相比的简单求和在数值总和结合的误差来实现double值。

这是一个 terminal operation

API Note:
  • Elements sorted by increasing absolute magnitude tend to yield more accurate results.
Returns
double the sum of elements in this stream

summaryStatistics

Added in API level 24
DoubleSummaryStatistics summaryStatistics ()

返回描述有关此流元素的各种摘要数据的DoubleSummaryStatistics 这是一个reduction的特例

这是一个 terminal operation

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

toArray

Added in API level 24
double[] toArray ()

返回包含此流的元素的数组。

这是一个 terminal operation

Returns
double[] an array containing the elements of this stream

Hooray!