Languages
[Edit]
EN

Java 8 - find first element in Stream

0 points
Created by:
Evie-Grace-Noble
621

In this article, we would like to show you how to find the first element in Stream in Java.

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

Syntax

Edit

Where:

  • Optional - container object which may or may not contain a non-null value.
    • If a value is present, isPresent() will return true and get() will return the value.
  • T - the type of objects.

Practical example

Edit

In this example, we use the findFirst() function on Stream of Strings.

Output:

References

Edit
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join