Java 8 Streams API Find Methods: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 22: Line 22:


{{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.


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
Optional<T> findFirst()
Optional<T> findFirst()
</syntaxhighlight>
</syntaxhighlight>

Revision as of 20:19, 28 March 2018

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