🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Optional.html
Optional (Java Platform SE 8 )
April 21, 2026 - This method is similar to map(Function), but the provided mapper is one whose result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional. ... the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is present, ...
🌐
Medium
medium.com › @AlexanderObregon › javas-optional-map-method-explained-0a19206d6704
Java’s Optional.map() Method Explained | Medium
November 9, 2024 - This approach can make code verbose and hard to read. Using Optional.map() helps simplify this by handling absent values automatically, skipping transformations when data is missing and returning an empty Optional instead.
Discussions

java - How to make `Map::get` return either an `Optional` of the found value or `Optional.empty()` - Stack Overflow
Optional#ofNullable, or better yet you can do some actions if the key is absent e.g. Map#computeIfAbsent More on stackoverflow.com
🌐 stackoverflow.com
java - Use of Optional in a map - Stack Overflow
Ok before I start explaining my question I want you to know that I know about the design idea behind Optional and that it isn't intended to be used in fields or collections, but I have programmed a... More on stackoverflow.com
🌐 stackoverflow.com
java - How does Optional.map() exactly works? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... According to javadoc, Optional.map() returns an Optional. More on stackoverflow.com
🌐 stackoverflow.com
option type - Existing way to Java map Optional<> onto object instead of to another object? - Stack Overflow
I'm familiar with Optional.map(mapper), which maps an optional object to something else. So if I wanted to do something with an optional Foo and a non-optional Bar, I could do: Optional More on stackoverflow.com
🌐 stackoverflow.com
🌐
Baeldung
baeldung.com › home › java › core java › guide to java optional
Guide To Java Optional | Baeldung
February 15, 2026 - The difference is that map transforms values only when they are unwrapped whereas flatMap takes a wrapped value and unwraps it before transforming it. Previously, we created simple String and Integer objects for wrapping in an Optional instance.
🌐
Verhoevenv
verhoevenv.github.io › 2016 › 08 › 30 › TIL-Optional-map.html
Today I Learned: Java 8's Optional.map is actually flatmap | Vincent Verhoeven's blog
August 30, 2016 - "A java Optional" should "map null to Optional.empty" in { val javaOptional = Optional.of("a value") val result = javaOptional.map(s2j(_ => null)) result.isPresent shouldBe false } "A scala Option" should "map null to Some(null)" in { val scalaOptional = Option("a value") val result = ...
Top answer
1 of 1
1

Edit

After watching Stuart Marks' (who works for the core libraries team in the JDK group at Oracle) talk "Optional – The Mother of All Bikesheds" from Devoxx 2016, you should jump to 54:04:

Why Not Use Optional in Fields?

  • More a style issue than a correctness issue
    • usually there's a better way to model absence of a value
    • use of Optional in fields often arises from slavish desire to eliminate nullable fields
    • remember, eliminating nulls isn't a goal of Optional
  • Using Optional in fields...
    • creates another object for every field
    • introduces a dependent load from memory on every field read
    • clutters up your code
    • to what benefit? ability to chain methods?

Original Post

According to IntelliJ's inspector (Preferences > Editor > Inspections > 'Optional' used as field or parameter type):

Optional was designed to provide a limited mechanism for library method return types where there needed to be a clear way to represent "no result". Using a field with type java.util.Optional is also problematic if the class needs to be Serializable, which java.util.Optional is not.

This also applies to collections in case you have to serialize them. Furthermore, have a look at these links:

  • Java 8 Optional: What's the Point?

    So to recap - in an attempt to get rid of NullPointerExceptions we have a new class that:

    • Throws NullPointerExceptions
    • Can itself be null, causing a NullPointerException
    • Increases heap size
    • Makes debugging more difficult
    • Makes serializing objects, say as an XML or JSON for an external client, much more difficult
  • Why java.util.Optional is broken

    The final irony is that by attempting to discourage nulls, the authors of this class have actually encouraged its use. I'm sure there are a few who will be tempted to simply return null from their functions in order to "avoid creating an unnecessary and expensive Optional reference", rather than using the correct types and combinators.

If you care about readability, you could also use @Nullable (available in Eclipse as well as in IntelliJ):

class ConnectionBox {
    @Nullable
    Connection connection;
    // ...
}

Alternatively, you can create an optional getter:

class ConnectionBox {
    Connection connection;
    // ...
    Optional<Connection> getConnection() {
        return Optional.ofNullable(connection);
    }
}
🌐
Dev.java
dev.java › learn › api › streams › optionals
Using Optionals - Dev.java
If there is a value in the optional, then the mapping function is called with this value. This mapping function creates a new key-value pair with the same key and this existing value.
Find elsewhere
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
🌐
X-Team
x-team.com › blog › using-optional-to-transform-your-java-code
Using Optional to Transform Your Java Code | X-Team
January 2, 2025 - Map() accepts a Function object that will be applied to the item if it is there. This example produces: Name is JOHN DOE Name is ALFRED NEUMAN Name is JOHN GALT · Function is another interface introduced with lambdas. It simply needs to be a member function of the type enclosed by the Optional, and cannot accept any parameters.
🌐
Laulem
laulem.com › en › dev › optional-usage-java-tutorial.html
Guide To Java Optional - LauLem.com
November 7, 2024 - Unlike traditional mapping, the Optional.flatMap method retrieves the value of the object and allows returning another instance of Optional (empty, different type, etc).
🌐
Oracle
docs.oracle.com › en › java › javase › 19 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 19 & JDK 19)
December 12, 2022 - This method is similar to map(Function), but the mapping function is one whose result is already an Optional, and if invoked, flatMap does not wrap it within an additional Optional. ... the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is ...
🌐
Reddit
reddit.com › r/java › using the optional class as it's meant to be used
r/java on Reddit: Using the Optional class as it's meant to be used
June 23, 2020 - However, I have used Optional extensively, as well as proper monads like VAVR's (formerly Javaslang) Option. In the end, I prefer Optional, even if its behavior is "wrong". In almost all cases, the behavior of Optional is what I'd want. If .map(x -> null) yields an empty Optional, that is quite fine by me.
🌐
Jsparrow
jsparrow.github.io › rules › optional-map.html
Use Optional::map | jSparrow Documentation
Optional<User> oUser = findById(userId); oUser.map(user -> user.getAddress()).ifPresent(address -> { sendGiftCard(address); sendAds(address); }); You can auto-refactor this with jSparrow. Drop this button to your Eclipse IDE workspace to install jSparrow for free: Need help? Check out our installation guide. ← Use Optional::ifPresentOrElse Use Offset Based String Methods →
🌐
Reddit
reddit.com › r/java › in the wild: java's optional for control-flow
r/java on Reddit: In the wild: Java's Optional for control-flow
April 16, 2023 - Instead of having "then" we have a line with implementation details of "then" via "map", and we have to name it "then" in our heads, which is just a bad non-reader-friendly way of naming methods and structuring code ... I agree! Typically this would start with a return value or parameter. I focused more on the example of wrapping a variable in an Optional purely for null-checking.
🌐
Readthedocs
java8tips.readthedocs.io › en › stable › optional.html
10. Handling nulls with Optional — Java 8 tips 1.0 documentation
Similar to Stream.map method, this is also commonly used as transformation function. This method supports post-processing on optional values, without the need to explicitly check for a return status. For example, the following code snippet traverses a stream of trades, selects first APAC trade encountered, and then returns the trade id, returning an Optional<String>:
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › util › Optional.html
Optional (Java SE 9 & JDK 9 )
This method is similar to map(Function), but the mapping function is one whose result is already an Optional, and if invoked, flatMap does not wrap it within an additional Optional. ... the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is ...