findFirst() gives you an Optional and you then have to decide what to do if it's not present. So findFirst().orElse(null) should give you the object or null if it's not present

You could just do a .get() on the Optional, but that could be regarded as poor practice since get() will throw an exception if Optional has no content. You should normally assert presence/absence of the Optional and decide what to do in each case (that's why it's there - so that you know something is truly optional and you have to determine what to do)

If you have an action you want to perform on object presence, and you don't want to do anything on absence, you can call .ifPresent() and provide a lambda as an argument. That will be called with the contained object, if present.

As of Java 9, a further solution would be to use Optional.ifPresentOrElse()

Answer from Brian Agnew on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › core java › guide to java optional
Guide To Java Optional | Baeldung
February 15, 2026 - Previously, we created simple String and Integer objects for wrapping in an Optional instance. However, frequently, we will receive these objects from an accessor of a complex object. To get a clearer picture of the difference, let’s have a look at a Person object that takes a person’s ...
🌐
Stacktraceguru
stacktraceguru.com › home › access value from optional object
Java 8 Optional ways to access value from optional object
November 7, 2018 - We should use this method on optional object only if we are sure that value is present and it is not empty optional. System.out.println(Optional.empty().get()); Exception in thread "main" java.util.NoSuchElementException: No value present · Optional<String> optional = Optional.of(“stacktraceguru”); System.out.println(optional .get()); Output : stacktraceguru · If value in optional can be null then we can use other methods like orElse(...) to access the value
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Optional.html
Optional (Java Platform SE 8 )
April 21, 2026 - Returns an empty Optional instance. No value is present for this Optional. ... Though it may be tempting to do so, avoid testing if an object is empty by comparing with == against instances returned by Option.empty(). There is no guarantee that it is a singleton.
🌐
CodingTechRoom
codingtechroom.com › question › retrieve-property-optional-object-javascript
How to Safely Retrieve a Property from an Optional Object in JavaScript? - CodingTechRoom
Attempting to access a property of an undefined object will result in a TypeError if not handled properly. Directly referencing properties without checks can lead to unwanted crashes. Using legacy methods like `if` statements can be cumbersome and less readable. Utilize optional chaining (?.) introduced in ES2020, which allows safe access to nested properties without manual checks.
🌐
Cppreference
cppreference.com › cpp › utility › optional
std::optional - cppreference.com
The object is initialized with/assigned from a value of type std::nullopt_t or an optional object that does not contain a value. The member function reset() is called. There are no optional references, functions, arrays, or (possibly cv-qualified) void; a program is ill-formed if it instantiates an optional with such a type. In addition, a program is ill-formed if it instantiates an optional with the (possibly cv-qualified) tag types std::nullopt_t or std::in_place_t. ... All requirements on the iterator types of a Container apply to the iterator type of optional as well.
🌐
Medium
medium.com › @nweligalla › optional-in-java-8-5fbf12c90bfe
Optional in Java 8. Java Optional, introduced in Java 8… | by Nayana Weligalla | Medium
January 23, 2024 - Java Optional enables you to obtain a default value when retrieving a value from an empty Optional object. To achieve this, you can use .orElse().
🌐
GeeksforGeeks
geeksforgeeks.org › java › optional-get-method-in-java-with-examples
Optional get() method in Java with examples - GeeksforGeeks
July 12, 2025 - Below programs illustrate get() method: Program 1: ... // Java program to demonstrate // Optional.get() method import java.util.*; public class GFG { public static void main(String[] args) { // create a Optional Optional<Integer> op = ...
Find elsewhere
🌐
Developer.com
developer.com › dzone › coding › java › how to use optionals in java
How to Use Optionals in Java
December 8, 2023 - Instead, we need a mechanism that allows a method developer to explicitly denote that the return value for a method may or may not be present. This mechanism was introduced in Java Development Kit (JDK) 7 in the form of the Optional class. This class acts as a wrapper for an object that may not be present.
🌐
TutorialsPoint
tutorialspoint.com › how-to-specify-optional-properties-in-typescript
How to specify optional properties in TypeScript?
Here, the question mark is used to make the property optional. The optionalProp is the optional property in the below syntax. interface sample { prop1: number; optionaProp?: string; } In this example, we have created the sample interface containing the two optional properties named optionalProp, and optionalProp2. In the object, we haven't added the optionalProp2, and still, we can compile our TypeScript code without any error. Also, we can't access the optional properties by taking the object as a reference if we haven't defined them inside the object.
🌐
Medium
cengizhanvarli.medium.com › std-optional-in-c-06127604815e
std::optional in C++ | by Cengizhan Varlı | Medium
October 21, 2023 - There is more than one way to access the value held by the optional object. We can access the object or the elements of that object with the content operator and arrow operator functions. If the optional object is empty, undefined behavior occurs.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-8-optional-class
Java 8 Optional Class - GeeksforGeeks
May 16, 2026 - Explanation: Here, Optional.of(str[2]) stores a value inside the Optional object. The get() method retrieves that value, hashCode() returns its hash value, and isPresent() checks whether a value exists. Since the value is present, it returns true.
🌐
LinkedIn
linkedin.com › pulse › optional-java-8-example-terala-chittibabu
Optional in Java 8 with example
July 12, 2022 - Optional can contain any object and hence it is declared with a generic type such as Optional<String>, Optional<Cat> etc., and it looks as below. With Optional, you can choose to get its value only if it is present, return a default value if ...
🌐
Blogger
javarevisited.blogspot.com › 2017 › 04 › 10-examples-of-optional-in-java-8.html
10 Examples of Optional in Java 8
Person p = new Person(); Optional<Person> op = Optional.of(p); How this code is different from any code without Optional? Well, the biggest advantage of this code is that it will throw a NullPointerException immediately if Person p is null, rather than throwing a NullPointerException when you try to access any field of the person object.
🌐
Stackify
stackify.com › optional-java
Understanding, Accepting and Leverage Optional in Java- Stackify
June 27, 2024 - Lastly, the new stream() method allows you to benefit from the extensive Stream API by transforming the instance to a Stream object. This will be an empty Stream if no value is present, or a Stream containing a single value – in case the Optional contains a non-null value.
🌐
DEV Community
dev.to › orfeo › making-object-properties-optional-42m6
Making Object Properties Optional - DEV Community
November 3, 2025 - Use ? to mark properties optional in type definitions. Avoid | undefined for optional properties because optionality already allows absence. Optional properties enable flexible object construction with any subset of the property keys.
🌐
Stack Overflow
stackoverflow.com › questions › 70433960 › access-to-an-optional-attribute-type-of-a-typescript-object
Access to an optional attribute type of a typescript object
More precisely, EnteringStyleParams and CenteringStyleParams objects will provide a style to a Target Object, that's why I need them to have the same style definition. ... I don't fully understand what your goal is. I don't get what Target is and what you are trying to do with it. Is your goal just to access the corresponding properties inside overlay.style depending on what the value of overlay.type is?