-
Notifications
You must be signed in to change notification settings - Fork 48
Optional.map vs Optional.flatMap
Original Discussion Link
Optional Javadocs API: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Optional.html
I personally felt that the descriptions are pretty cryptic and hard to understand, so much so that it was easier for me to understand by reading OpenJDK's source code. So I hope this ELI5 might be useful for others like me.
Official description of Optional.map:
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.
Official description of Optional.flatMap:
If a value is present, returns the result of applying the given Optional-bearing mapping function to the value, otherwise returns an empty Optional.
ELI5 description of Optional.map:
Let us have
Optional<MyClass> myOp
.myOp.map
is meant to take in a function that does not return an Optional. If there is a value inside the myOp, apply the function to the value. Since we do not know if the result of the function will *chibabom or not, we wrap the result in an Optional for safety. If there is no value inside the myOp, we return and empty Optional.
ELI5 description of Optional.flatMap:
Let us have
Optional<MyClass> myOp
.myOp.flatMap
is meant to take in a function that already returns an Optional. As a matter of fact, it will not compile if the mapper function does not return an Optional. If there is a value inside the myOp, apply the function to the value. Since we know that the function already returns an Optional, there is no fear that it will *chibabom. Therefore wrapping the result in another Optional is redundant, and we can return it directly. If there is no value inside the myOp, we return and empty Optional.
*chibabom = explode, 爆炸, null value, invalid value, throw error
Optional, Optional.map, Optional.flatMap
OpenJDK's Optional.map and Optional.flatMap source code:
Comic Representation:
Peer Learning
Codecrunch Contributions
Piazza Contributions
Wiki Contributions
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Sunfire
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
CS2030 Contents
Lecture 1 SummaryCompile-run vs Run-time Summary
Quick Guide To Abstraction
Generics and Variance of Types
Comparable vs Comparator
Summary of completable future
CS2030S Notes
ELI5 Optional.map vs Optional.flatMap
PECS Example Code
Java Collection Framework (Iterator)
Generic
Generic Type Parameter and Generic Wildcard
Calculator
Lambda-Expression
Single Abstract Method (SAM)
Method Reference
Functional Interfaces 2
Simple Usage of Sandbox
Associative-but-not-commutative
Higher Order function
Functional Programming
Calculator With Functor
Eager Evaluation VS Lazy Evaluation
Simple Usage of Lazy Evaluation
Lazy Evaluation for LazyList
Lazy Evaluation for BinaryTree
Stream
Parallel Stream
Optional
Simple Usage of Stream
Asynchronous Programming
Notes on CompletableFuture
Notes on CompletableFuture 2
Simple Usage of CompletableFuture
Mind Map
Exception Handling
Links
CS2030 Java Style Guide
CS2030 Javadoc Specification
JDK 11 Download Link
JDK 11 API Docs
Codecrunch
Piazza Forum