Java 8 Streams API Find Methods

From NovaOrdis Knowledge Base
Revision as of 20:20, 28 March 2018 by Ovidiu (talk | contribs) (→‎findAny)
Jump to navigation Jump to search

Internal

Overview

Any of these operations are terminal, in that they return a non-stream result.

Methods

findAny

https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#findAny()

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

The stream pipeline will be optimized to perform a single pass and finish as soon as a result is found by using short-circuiting.

The behavior of this operation is explicitly nondeterministic; it is free to select any element in the stream. This is to allow for maximal performance in parallel operations; the cost is that multiple invocations on the same source may not return the same result. If a stable result is desired, use findFirst() instead.

Optional<T> findAny()

findFirst

https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#findFirst()

Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no encounter order, then any element may be returned.

Optional<T> findFirst()