Most visited

Recently visited

Added in API level 24

LongStream

public interface LongStream
implements BaseStream<LongLongStream>

java.util.stream.LongStream


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

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

long sum = widgets.stream()
                       .filter(w -> w.getColor() == RED)
                       .mapToLong(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 LongStream.Builder

LongStream可变建设者。

Public methods

abstract boolean allMatch(LongPredicate predicate)

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

abstract boolean anyMatch(LongPredicate predicate)

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

abstract DoubleStream asDoubleStream()

返回由该流的元素组成的 DoubleStream ,转换为 double

abstract OptionalDouble average()

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

abstract Stream<Long> boxed()

返回由此流的元素组成的 Stream ,每个元素 Long一个 Long

static LongStream.Builder builder()

返回 LongStream的构建器。

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

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

static LongStream concat(LongStream a, LongStream b)

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

abstract long count()

返回此流中元素的数量。

abstract LongStream distinct()

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

static LongStream empty()

返回一个空的顺序 LongStream

abstract LongStream filter(LongPredicate predicate)

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

abstract OptionalLong findAny()

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

abstract OptionalLong findFirst()

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

abstract LongStream flatMap(LongFunction<? extends LongStream> mapper)

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

abstract void forEach(LongConsumer action)

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

abstract void forEachOrdered(LongConsumer action)

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

static LongStream generate(LongSupplier s)

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

static LongStream iterate(long seed, LongUnaryOperator f)

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

abstract PrimitiveIterator.OfLong iterator()

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

abstract LongStream limit(long maxSize)

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

abstract LongStream map(LongUnaryOperator mapper)

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

abstract DoubleStream mapToDouble(LongToDoubleFunction mapper)

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

abstract IntStream mapToInt(LongToIntFunction mapper)

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

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

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

abstract OptionalLong max()

返回描述此流的最大元素的 OptionalLong ,如果此流为空,则返回一个空的可选项。

abstract OptionalLong min()

返回描述此流的最小元素的 OptionalLong ,如果此流为空,则返回一个空的可选项。

abstract boolean noneMatch(LongPredicate predicate)

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

static LongStream of(long... values)

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

static LongStream of(long t)

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

abstract LongStream parallel()

返回平行的等效流。

abstract LongStream peek(LongConsumer action)

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

static LongStream range(long startInclusive, long endExclusive)

返回从 startInclusive (含)至 endExclusivestartInclusive )的顺序排列的 LongStream ,增量为 1

static LongStream rangeClosed(long startInclusive, long endInclusive)

返回从 startInclusive (含)至 endInclusive (含)的顺序排列的 LongStream ,增量为 1

abstract OptionalLong reduce(LongBinaryOperator op)

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

abstract long reduce(long identity, LongBinaryOperator op)

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

abstract LongStream sequential()

返回顺序的等效流。

abstract LongStream skip(long n)

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

abstract LongStream sorted()

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

abstract Spliterator.OfLong spliterator()

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

abstract long sum()

返回此流中元素的总和。

abstract LongSummaryStatistics summaryStatistics()

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

abstract long[] toArray()

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

Inherited methods

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

Public methods

allMatch

Added in API level 24
boolean allMatch (LongPredicate 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 LongPredicate: 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 (LongPredicate 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 LongPredicate: 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

asDoubleStream

Added in API level 24
DoubleStream asDoubleStream ()

返回由该流的元素组成的 DoubleStream ,转换为 double

这是一个 intermediate operation

Returns
DoubleStream a DoubleStream consisting of the elements of this stream, converted to double

average

Added in API level 24
OptionalDouble average ()

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

这是一个 terminal operation

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<Long> boxed ()

返回由此流的元素组成的 Stream ,每个元素装箱到 Long

这是一个 intermediate operation

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

builder

Added in API level 24
LongStream.Builder builder ()

返回 LongStream的构建器。

Returns
LongStream.Builder a stream builder

collect

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

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

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

reduce(long, LongBinaryOperator)一样, 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 ObjLongConsumer: 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
LongStream concat (LongStream a, 
                LongStream 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 LongStream: the first stream
b LongStream: the second stream
Returns
LongStream the concatenation of the two input streams

count

Added in API level 24
long count ()

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

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

这是一个 terminal operation

Returns
long the count of elements in this stream

distinct

Added in API level 24
LongStream distinct ()

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

这是一个 stateful intermediate operation

Returns
LongStream the new stream

empty

Added in API level 24
LongStream empty ()

返回一个空的顺序 LongStream

Returns
LongStream an empty sequential stream

filter

Added in API level 24
LongStream filter (LongPredicate predicate)

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

这是一个 intermediate operation

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

findAny

Added in API level 24
OptionalLong findAny ()

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

这是一个 short-circuiting terminal operation

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

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

也可以看看:

findFirst

Added in API level 24
OptionalLong findFirst ()

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

这是一个 short-circuiting terminal operation

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

flatMap

Added in API level 24
LongStream flatMap (LongFunction<? extends LongStream> mapper)

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

这是一个 intermediate operation

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

也可以看看:

forEach

Added in API level 24
void forEach (LongConsumer action)

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

这是一个 terminal operation

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

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

forEachOrdered

Added in API level 24
void forEachOrdered (LongConsumer action)

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

这是一个 terminal operation

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

也可以看看:

generate

Added in API level 24
LongStream generate (LongSupplier s)

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

Parameters
s LongSupplier: the LongSupplier for generated elements
Returns
LongStream a new infinite sequential unordered LongStream

iterate

Added in API level 24
LongStream iterate (long seed, 
                LongUnaryOperator f)

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

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

Parameters
seed long: the initial element
f LongUnaryOperator: a function to be applied to to the previous element to produce a new element
Returns
LongStream a new sequential LongStream

iterator

Added in API level 24
PrimitiveIterator.OfLong iterator ()

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

这是一个 terminal operation

Returns
PrimitiveIterator.OfLong the element iterator for this stream

limit

Added in API level 24
LongStream 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(LongSupplier)) 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
LongStream the new stream
Throws
IllegalArgumentException if maxSize is negative

map

Added in API level 24
LongStream map (LongUnaryOperator mapper)

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

这是一个 intermediate operation

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

mapToDouble

Added in API level 24
DoubleStream mapToDouble (LongToDoubleFunction mapper)

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

这是一个 intermediate operation

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

mapToInt

Added in API level 24
IntStream mapToInt (LongToIntFunction mapper)

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

这是一个 intermediate operation

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

mapToObj

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

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

这是一个 intermediate operation

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

max

Added in API level 24
OptionalLong max ()

返回描述此流的最大元素的OptionalLong ,或者如果此流为空,则返回一个空的可选项。 这是reduction的特例 ,相当于:

return reduce(Long::max);
 

这是一个 terminal operation

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

min

Added in API level 24
OptionalLong min ()

返回描述此流的最小元素的OptionalLong ,或者如果此流为空,则返回一个空的可选元素。 这是reduction的特例 ,相当于:

return reduce(Long::min);
 

这是一个 terminal operation

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

noneMatch

Added in API level 24
boolean noneMatch (LongPredicate 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 LongPredicate: 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
LongStream of (long... values)

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

Parameters
values long: the elements of the new stream
Returns
LongStream the new stream

of

Added in API level 24
LongStream of (long t)

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

Parameters
t long: the single element
Returns
LongStream a singleton sequential stream

parallel

Added in API level 24
LongStream parallel ()

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

这是一个 intermediate operation

Returns
LongStream a parallel stream

peek

Added in API level 24
LongStream peek (LongConsumer 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:
    LongStream.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 LongConsumer: a non-interfering action to perform on the elements as they are consumed from the stream
Returns
LongStream the new stream

range

Added in API level 24
LongStream range (long startInclusive, 
                long endExclusive)

返回从 startInclusive (含)至 endExclusive (不包括)的连续排序 LongStream ,增量为 1

API Note:
  • 可以使用 for循环按顺序产生递增值的等效序列,如下所示:

    for (long i = startInclusive; i < endExclusive ; i++) { ... }
     
Parameters
startInclusive long: the (inclusive) initial value
endExclusive long: the exclusive upper bound
Returns
LongStream a sequential LongStream for the range of long elements

rangeClosed

Added in API level 24
LongStream rangeClosed (long startInclusive, 
                long endInclusive)

返回从 startInclusive (含)至 endInclusive (含)的连续排序 LongStream ,增量为 1

API Note:
  • 可以使用 for循环按顺序产生递增值的等效序列,如下所示:

    for (long i = startInclusive; i <= endInclusive ; i++) { ... }
     
Parameters
startInclusive long: the (inclusive) initial value
endInclusive long: the inclusive upper bound
Returns
LongStream a sequential LongStream for the range of long elements

reduce

Added in API level 24
OptionalLong reduce (LongBinaryOperator op)

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

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

accumulator函数必须是 associative函数。

这是一个 terminal operation

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

也可以看看:

reduce

Added in API level 24
long reduce (long identity, 
                LongBinaryOperator op)

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

long result = identity;
     for (long element : this stream)
         result = accumulator.applyAsLong(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:
    long sum = integers.reduce(0, (a, b) -> a+b);
     
    or more compactly:
    long sum = integers.reduce(0, Long::sum);
     

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

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

也可以看看:

sequential

Added in API level 24
LongStream sequential ()

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

这是一个 intermediate operation

Returns
LongStream a sequential stream

skip

Added in API level 24
LongStream 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(LongSupplier)) 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
LongStream the new stream
Throws
IllegalArgumentException if n is negative

sorted

Added in API level 24
LongStream sorted ()

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

这是一个 stateful intermediate operation

Returns
LongStream the new stream

spliterator

Added in API level 24
Spliterator.OfLong spliterator ()

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

这是一个 terminal operation

Returns
Spliterator.OfLong the element spliterator for this stream

sum

Added in API level 24
long sum ()

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

return reduce(0, Long::sum);
 

这是一个 terminal operation

Returns
long the sum of elements in this stream

summaryStatistics

Added in API level 24
LongSummaryStatistics summaryStatistics ()

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

这是一个 terminal operation

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

toArray

Added in API level 24
long[] toArray ()

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

这是一个 terminal operation

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

Hooray!