DEV Community

Discussion on: Explain Java 8's Optional Like I'm Five

Collapse
bizzy237 profile image
Yury

other Optional methods

Optional::orElse - you have another cup which you will use if the box is empty (or you don't have a cup but act like you do)
Optional::orElseGet - you don't have another cup but you know where to get it (and you throw a fit if you don't get a cup there)
Optional::filter - you only need coffee if it has no sugar
Optional::map - you have a kettle and want to make a cup of coffee
Optional::flatMap - you have a kettle and want to make a cup of coffee but there might be no cups in the cupboard
Optional::ifPresent - do something with a cup