Java 8 Streams API Find Methods: Difference between revisions
Line 15: | Line 15: | ||
Returns an Optional describing some element of the stream, or an empty Optional if the stream is empty. | 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 stream pipeline will be optimized to perform a single pass and finish as soon as a result is found by using [[Java_8_Streams_API#Short-Circuiting|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|findFirst()]] instead. | 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|findFirst()]] instead. |
Revision as of 20:21, 28 March 2018
Internal
Overview
Any of these operations are terminal, in that they return a non-stream result.
Methods
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
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()