Return Optional as it is if the returned Optional has value, else call another function [duplicate]
Asked Answered
S

5

5

I have two methods func1 and func2 which return Optional. Return Optional as it is if the returned Optional from func1 has value, else call func2 and return its Optional. One way is to use if-else.

Optional<MyObject> opt1 = func1();
if (opt1.isPresent()) {
    return opt1;
}
return func2();

However, I wish to achieve it using Java 8 Optional and avoid if-else.

Something like:

return Optional.of(obj1) 
        .flatMap_returnOptionalIfvaluePresent(func1)
        .orElseReturnOptionalFromThis(func2)

Can anyone suggest a good way for it?

Stertor answered 18/1, 2019 at 9:43 Comment(4)
What is func1 and func2? If they are methods, please reformat your code to be valid. Provide the expected inputs and desired outputs. Provide the if-else to make the question clearer.Canfield
Why would you want to avoid a clear and simple if-else for a hard-to-read way ?Vulturine
This has the potential to be a good question but it's currently not clear what you want. Post your code that uses if-else so that its obvious what you're trying to achieve.Emboss
@Emboss I am with you here, if the OP comes to explain more, this has the potential to be a fairly good questionBriscoe
W
7

Edit: or

Java 9 and later offer a very elegant solution:

Optional<String> myFunc() {
    return func1().or(this::func2);
}

or (introduced in Java 9) does exactly what you asked for: If the Optional returned from func1 has a value, it is returned (or an equivalent Optional). If not, the supplier (here func2()) is invoked to get an Optional, which is then returned.

Java 8

There are several ways. In Java 8 I prefer to take the values out of the Optionals from func1 and func2:

Optional<String> myFunc() {
    String returnValue = func1().orElse(func2().orElse(null));
    return Optional.ofNullable(returnValue);
}

Edit 2: @Holger’s alternative suggestion in a comment is good enough for quoting within the answer (Holger, you may have posted it as a comment only because the question is closed and you therefore could not post your own answer):

    return func1().map(Optional::of).orElse(func2());

It goes the opposite way: The mapping using Optional::of wraps the Optional from func1 inside yet an Optional only if it has a value, from which orElse unwraps it again.

If the call to func2 is expensive, you may want to avoid it when it’s not needed (when func1 supplies a value):

    String returnValue = func1().orElseGet(() -> func2().orElse(null));

Or in Holger’s version:

    return func1().map(Optional::of).orElseGet(this::func2);

isPresent used in a couple of other answers is so low-level, I seldom use it and only as a last resort.

Whomp answered 18/1, 2019 at 9:56 Comment(7)
Is there any Optional API to eliminate return Optional.ofNullable(returnValue);Stertor
No, @adimoh, but you may of course inline it if you prefer: return Optional.ofNullable(func1().orElse(func2().orElse(null)));.Whomp
@OleV.V. the java-9 or is the way to go here, but at the same time I see nothing wrong with something like public static <T> Optional<T> find(Optional<T> left, Optional<T> right) { return left.isPresent() ? left : right; }.. 1+Briscoe
Using func1().orElseGet(() -> func2().orElse(null)) will bring a slight improvement as func2 wouldn't be executed eagerly. Good solutions though.Dr
Yes, @ernest_k, that’s exactly what I say near the bottom of the answer. Great minds think alike. :-)Whomp
@Emboss A very near duplicate. That question doesn’t have the method calls to func1 and func2, at least not explicitly (likely the two Optionals would come from method calls, though), which does add a little twist to this question the way I see it.Whomp
return func1().map(Optional::of).orElse(func2()); or, for an expensive func2 operation, return func1().map(Optional::of).orElseGet(this::func2);Fulvi
D
2

If you're using java 9+, you can just use Optional.or:

return func1().or(() -> func2());

In Java 8, you may need to create a new Optional from results (use orElseGet to avoid eager execution):

return Optional.ofNullable(func1().orElseGet(() -> func2().orElse(null)));
Dr answered 18/1, 2019 at 10:15 Comment(0)
M
0

What about something like

Object value1 = func1();
return value1.isPresent() ? value1 : func2();

Unless I've misunderstood what you're after that will return the value from the first function if it's present, and if it doesn't exist then call func2() to return the second optional. You could probably simplify it further to func1().isPresent but it made it a little less clear.

Mungovan answered 18/1, 2019 at 9:51 Comment(0)
H
0

It would be something lke this, if i usterstand everything correctly:

Optional.of(obj1)
        .filter(obj -> func1().isPresent())
        .orElse(func2());
Hevesy answered 18/1, 2019 at 9:54 Comment(2)
orElse return the value of Optional. I want to return Optional itself.Stertor
orElse returns result of func2() function and it returns Optional, doesn't it?Hevesy
M
0

Here is a more generic version, working with n functions:

private static <T> Optional<T> firstPresent(Supplier<Optional<T>>... optionals) {
    return Stream.of(optionals)
            .map(Supplier::get)
            .filter(Optional::isPresent)
            .findFirst()
            .orElse(Optional.empty());
}

To be used like this:

    Optional<String> result = firstPresent(() -> func1(null), () -> func2("abc"));
Madeira answered 18/1, 2019 at 10:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.