← Quiz: Java Optional: all 10 questions
Quiz: Java Optional · Question 5 of 10
When should you use flatMap instead of map on an Optional?
When should you use flatMap instead of map on an Optional?
- A.When the value is a List
- B.When the function you pass already returns an Optional
- C.When the Optional is empty
- D.When you want to sort the value
Answer & explanation
Correct answer: When the function you pass already returns an Optional
flatMap avoids Optional<Optional<T>> by not wrapping the function's result again.
Can you get the rest right without peeking?
Take the timed Quiz: Java Optional quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →