The whole chain of operations returns a String:

  • The first step (map(...)) maps the Optional<User> to an Optional<String>.
  • The second step (orElseThrow(...)) unwraps the Optional<String>, thus returning a String (or throwing an IllegalArgumentException, if empty).

We can find the source code of Optional::map here.

Answer from Turing85 on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Optional.html
Optional (Java Platform SE 8 )
April 21, 2026 - an Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional ... If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.
🌐
Medium
medium.com › @AlexanderObregon › javas-optional-map-method-explained-0a19206d6704
Java’s Optional.map() Method Explained | Medium
November 9, 2024 - Java’s Optional class provides a safer way to handle potentially null values, making it possible to avoid the dreaded NullPointerException. The Optional.map() method, in particular, offers an efficient approach to transform or apply functions to values that may or may not be present.
🌐
Baeldung
baeldung.com › home › java › core java › guide to java optional
Guide To Java Optional | Baeldung
February 15, 2026 - Notice that the filter method simply performs a check on the value and returns an Optional describing this value only if it matches the given predicate. Otherwise returns an empty Optional. The map method however takes the existing value, performs a computation using this value, and returns the result of the computation wrapped in an Optional object:
🌐
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
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional. It makes total sense for Java. The class invariant is that an Optional can never hold a null value, so either the map ...
🌐
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 - 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
Find elsewhere
🌐
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.
🌐
Bootstrap
getbootstrap.com
Bootstrap · The most popular HTML, CSS, and JS library in the world.
Bootstrap utilizes Sass for a modular and customizable architecture. Import only the components you need, enable global options like gradients and shadows, and write your own CSS with our variables, maps, functions, and mixins.
🌐
Coursera
coursera.org › browse › computer science › algorithms
Algorithms, Part I | Coursera
July 7, 2025 - Stack and Queue Applications (optional)•13 minutes · Overview•1 minute · Lecture Slides•0 minutes · Interview Questions: Stacks and Queues (ungraded)•0 minutes · Deques and Randomized Queues•480 minutes · Module 5•1 hour to completeModule details · We introduce the sorting problem and Java's Comparable interface.
Rating: 4.9 ​ - ​ 12.1K votes
🌐
Modrinth
modrinth.com › mod › xaeros-world-map
Xaero's World Map - Minecraft Mod
Resource pack support. Changing the resource packs causes the whole world map to regenerate. Cave dimensions support (for example the Nether). Optional vanilla maps mode, which makes the world map look like the vanilla paper maps.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › objects.html
TypeScript: Documentation - Object Types
All optionality really says is that if the property is set, it better have a specific type. We can also read from those properties - but when we do under strictNullChecks, TypeScript will tell us they’re potentially undefined. ... In JavaScript, even if the property has never been set, we can still access it - it’s just going to give us the value undefined.
🌐
Google
developers.google.com › google maps platform › web › maps javascript api › place id finder
Place ID Finder | Maps JavaScript API | Google for Developers
The sample then displays an info window containing // the place ID and other information about the place that the user has // selected. async function init(): Promise<void> { // Request needed libraries. const [{ InfoWindow }, { AdvancedMarkerElement }] = await Promise.all([ google.maps.importLibrary('maps'), google.maps.importLibrary('marker'), google.maps.importLibrary('places'), ]); const mapElement = document.querySelector('gmp-map')!; const map = mapElement.innerMap; const placeAutocomplete = document.querySelector('gmp-place-autocomplete')!; // Set the map options.
🌐
Dev.java
dev.java › learn › api › streams › optionals
Using Optionals - Dev.java
The Optional class also provides patterns so that you can integrate your optional objects' processing with stream processing. It has methods that directly correspond to methods from the Stream API that you can use to process your data in the same way, and that will seamlessly integrate with streams. Those methods are map(), filter(), and flatMap().
🌐
Readthedocs
java8tips.readthedocs.io › en › stable › optional.html
10. Handling nulls with Optional — Java 8 tips 1.0 documentation
Scala has a safe way to navigate through values, Google’s Guava library and Groovy language has same construct as Java Optional, so you can assume java Optional is inspired from them. Optional provides three basic methods: map, flatMap and filter to perform any kind of common task.
🌐
Minecraft Wiki
minecraft.fandom.com › wiki › Server.properties
server.properties – Minecraft Wiki
March 18, 2026 - server.properties is the file that stores all the settings for a multiplayer (Minecraft or Minecraft Classic) server. When editing server.properties, it is important that the same structure as the original is used, although the order of the lines is arbitrary. The text before the equal sign is...
🌐
OneCompiler
onecompiler.com › java
Java Online Compiler
This framework also defines map interfaces and several classes in addition to Collections.
🌐
Swift Forums
forums.swift.org › using swift
Optional .map() vs. flatMap(): why not always use .flatMap()? - Using Swift - Swift Forums
March 24, 2021 - It seems Optional.map() when the transform closure returns nil, then result is Optional(nil) , which is same as Optional.some(nil)? Still a nil but is wrap up an optional? But Optional.flatMap() when the transform closure returns nil, the result is just nil, which is simpler.
🌐
Frankel
blog.frankel.ch › optional-stream
Optional.stream()
February 21, 2021 - public BigDecimal getOrderPrice(Long orderId) { return Optional.ofNullable(orderId) (1) .map(orderRepository::findByOrderId) (2) .flatMap(lines -> { (3) BigDecimal sum = lines.stream() .map(OrderLine::getPrice) .reduce(BigDecimal.ZERO, BigDecimal::add); return Optional.of(sum); (4) }).orElse(BigDecimal.ZERO); (5) } Optional makes the code less readable! I believe that readability should trump code style every single time. Fortunately, Optional offers a stream() method (since Java 9).