Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELI5: Optional.map vs Optional.flatMap #180

Closed
TypeDefinition opened this issue Oct 11, 2020 · 2 comments
Closed

ELI5: Optional.map vs Optional.flatMap #180

TypeDefinition opened this issue Oct 11, 2020 · 2 comments
Labels
🖱️ Java General Questions with regards to Java or its API

Comments

@TypeDefinition
Copy link

TypeDefinition commented Oct 11, 2020

Description

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.

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.

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

Topic:

Optional, Optional.map, Optional.flatMap

Screenshots (if any):

OpenJDK's Optional.map and Optional.flatMap source code:
image

Comic Representation:
Java_Optional_Map_Comic

@TypeDefinition TypeDefinition added the 🖱️ Java General Questions with regards to Java or its API label Oct 11, 2020
@TypeDefinition TypeDefinition changed the title ELI5: Optional.map vs Optional.flatmap ELI5: Optional.map vs Optional.flatMap Oct 16, 2020
@jedlwk
Copy link

jedlwk commented Nov 29, 2020

@TypeDefinition Thanks bro.. this clears up a lot of my confusion ;)

You can consider transferring this to the wiki page :D

@TypeDefinition
Copy link
Author

TypeDefinition commented Nov 29, 2020

@TypeDefinition Thanks bro.. this clears up a lot of my confusion ;)

You can consider transferring this to the wiki page :D

@jedlwk Have added to wiki.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖱️ Java General Questions with regards to Java or its API
Projects
None yet
Development

No branches or pull requests

2 participants