Was working on something, and I wanted to combine two optionals. In my case, I know that only one of the two will be present (or none), so combining them would be nice.
I'm not a big fan of using .ifPresent() and .get() combos. So, let's try streams.
Using streams, this looks something like:
Luckily we have a Optional.or() nowadays (Since Java 9), that I haven't used before.
It looks a lot better:
References
- Baeldung - Guide To Java Optional
- https://www.baeldung.com/java-optional
- Combine two Java Optionals
- https://www.agalera.eu/combine-two-optionals/
No comments:
Post a Comment