• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java 17 OCP: I try to understand "Chaining Optionals"

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm busy with reading and do some practicing for Java 17 OCP. I'm busy in Chapter 10 "Channing Optionals".
Suddenly out of the blue, there is line represented like
I can follow that flatMap must be used instead off map().

Than I was asking myself what would you do with it? What is possible to use it that makes the live easier...

I have made something, but that doesn't make really sense but, at least it compiles and it runs, but is just returning what passed in:


No I try to do something like counting the length of a String, but this doesn't compile:


And this overloaded method calling by:

An Optional like Optional<Integer> does have one value, right. I mean it is not a List or Array something like that.
It's for me very hard to understand what can you do with it? In which situation would you use it?

For diehard java developers they think a stupid question, you must be creative to think what you should do with it, but as said, on this moment I don't see the advantage.
 
Bartender
Posts: 15737
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's say you have a type that has an optional property, and then the return type of THAT property also has an optional property:

Now, if you wanted to know the favorite snack of your favorite animal, you could do something like this:

This would be really cumbersome though, because snack is now an Optional wrapped in another Optional. Instead, you'd like it to be an Optional<Snack> that is empty when either the person has no favorite animal, or the animal has no favorite snack:

Admittedly, this doesn't happen a lot. I've only needed to do this in my code a handful of  times. Just remember, if you ever run into a situation where you end up with an Optional wrapped in another Optional, it's likely you should have called flatMap() somewhere instead of map().
 
Replace the word "snake" with "danger noodle" in all tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic