Java 8 Streams API Find Methods: Difference between revisions
(2 intermediate revisions by the same user not shown) | |||
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. | ||
Line 27: | Line 27: | ||
{{External|https://docs.oracle.com/javase/10/docs/api/java/util/stream/Stream.html#findFirst()}} | {{External|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. | Returns an Optional describing the first element of this stream, or an empty Optional if the stream is empty. If the stream has no [[Java_8_Streams_API#Encounter_Order|encounter order]], then any element may be returned. | ||
Finding the first element is more constraining in parallel. If you don't care about which element is returned, use [[#findAny|findAny]]. | |||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
Optional<T> findFirst() | Optional<T> findFirst() | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 20:26, 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.
Finding the first element is more constraining in parallel. If you don't care about which element is returned, use findAny.
Optional<T> findFirst()