🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Optional.html
Optional (Java Platform SE 8 )
April 21, 2026 - If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional.
🌐
Baeldung
baeldung.com › home › java › core java › guide to java optional
Guide To Java Optional | Baeldung
February 15, 2026 - We will look at the map() method in detail in the next section. First of all, if a null object is passed to this method, we don’t expect any problem. Secondly, the only logic we write inside its body is exactly what the method name describes — price-range check. Optional takes care of the rest:
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › util › Optional.html
Optional (Java SE 9 & JDK 9 )
an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 17 & JDK 17)
April 21, 2026 - If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
🌐
Oracle
docs.oracle.com › en › java › javase › 14 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 14 & JDK 14)
If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
🌐
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 - If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
🌐
Oracle
docs.oracle.com › en › java › javase › 12 › docs › api › java.base › java › util › Map.html
Map (Java SE 12 & JDK 12 )
Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed.
🌐
Oracle
docs.oracle.com › en › java › javase › 20 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 20 & JDK 20)
July 10, 2023 - If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Map.html
Map (Java Platform SE 8 )
April 21, 2026 - Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that (key==null ? k==null : key.equals(k)), that mapping is removed.
Find elsewhere
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Map.html
Map (Java SE 11 & JDK 11 )
January 20, 2026 - Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 11 & JDK 11 )
January 20, 2026 - 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.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › util › Optional.html
Optional (Java SE 10 & JDK 10 )
an Optional describing the result of applying a mapping function to the value of this Optional, if a value is present, otherwise an empty Optional
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › › api › java.base › java › util › Optional.html
Optional (Java SE 21 & JDK 21)
January 20, 2026 - If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
🌐
javaspring
javaspring.net › blog › java-optional-map
Mastering Java Optional Map: A Comprehensive Guide — javaspring.net
The map method of the Java Optional class is a powerful tool for performing operations on the value inside an Optional container in a null-safe way. It allows you to chain multiple operations and handle null values gracefully.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › util › Map.html
Map (Java SE 10 & JDK 10 )
Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed.
🌐
Oracle
docs.oracle.com › en › java › javase › 12 › docs › api › java.base › java › util › Optional.html
Optional (Java SE 12 & JDK 12 )
If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional.
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);
    }
}
🌐
Oracle
docs.oracle.com › en › java › javase › 26 › docs › api › java.base › java › util › Map.html
Map (Java SE 26 & JDK 26)
March 16, 2026 - Attempts to compute a mapping for the specified key and its current mapped value, or null if there is no current mapping (optional operation). ... If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function ...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Map.html
Map (Java SE 21 & JDK 21)
January 20, 2026 - Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › util › Map.html
Map (Java SE 9 & JDK 9 )
Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that Objects.equals(key, k), that mapping is removed.