Functional style of Java 8's Optional.ifPresent and if-not-Present?
With ifPresentOrElse
, you can execute an action if a value is present (System.out::println
), or perform an alternative action when absent (System.err.println("Missing")
). This is a functionality of elegance and clarity.
Performing dual actions with Optional
To enhance the ifPresentOrElse
behaviour, customize it for the business logic. For example, consider the scenario where you handle an entity in a database. If the Optional
entity is present, update it; otherwise create a new one.
Towards maintainable code with the OptionalConsumer
To manage reusable ifPresent/ifNotPresent workflows more efficiently, we can define a custom OptionalConsumer
class. This allows us to maintain a clean encapsulation of the ifPresent and ifNotPresent logic as well as reusable code.
Using the OptionalConsumer
in a builder-pattern like way, ending with an execute()
call.
This adds to the overall readability and maintainability of your codebase.
Applying transformations to Optionals
To perform transformations based on the presence or absence of a value, Optional.map
followed by orElse
is your key to success.
Filter unwanted Optional values
Use Optional.filter
to apply predicates before deciding on further conditional actions.
This way, you can filter optional's value based on a condition before making further decisions.
Handling empty Optionals without stress
For the scenarios where an Optional
is expected to contain a value and its absence is considered exceptional, orElseThrow
allows you to throw an exception as easy as a pie.
Thus, not only does it provide clear flow of control but can also communicate specific exceptional conditions.
Functional Coding with Optionals
Whether you use Java 9's ifPresentOrElse
or the functionalities of Java 8, functional style injects a new level of precision and clarity in your code. It provides a clear handle to deal with values that may or may not be present, promoting cleaner and more intuitive control flows.
Was this article helpful?