🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.When the value is a List
  2. B.When the function you pass already returns an Optional
  3. C.When the Optional is empty
  4. 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 →