Looking for a way to chain optionals so that the first one that is present is returned. If none are present Optional.empty()
should be returned.
Assuming I have several methods like this:
Optional<String> find1()
I'm trying to chain them:
Optional<String> result = find1().orElse( this::find2 ).orElse( this::find3 );
but of course that doesn't work because orElse
expects a value and orElseGet
expects a Supplier
.
Supplier
would be.orElseGet()
. – Burthen