1

I am trying to find the most concise (and meaningful) way of using Java Optional, to read the first value off a Optional<String> and return the String if exists, or return "NOT_FOUND". Here is the code I am working with:

public static String getValue(Optional<String> input) {
    return input.ifPresent(val -> val.get()).orElse("NOT_FOUND")
}

The methods of Optional apparently have very specific purposes but the API has left me confused.

Update (4/13/2018): The code in my question is incorrect, because if I regarded val as the value inside the Optional, then val.get() does not make any sense. Thanks for pointing that out, @rgettman.

Also, I added another part to my question in the accepted answer's comments, i.e. I needed a way to manipulate the String value, if present, before returning. The orElse("NOT_FOUND") is still applicable, if the Optional does not contain a value. So what is an acceptable use of the Optional API to achieve the following?

public static String getValue(Optional<String> input) {
    return input.isPresent() ? input.get().substring(0,7).toUpperCase() : "NOT_FOUND";
}

@Aominè's answer and follow up comments addressed both parts of this question.

Web User
  • 7,438
  • 14
  • 64
  • 92
  • 1
    What is `val`? Are you attempting to use the return value of `val.get()` somehow? – rgettman Apr 12 '18 at 23:34
  • Actually, `val` is the value in the Optional, so`val.get()` wouldn't even compile. I am trying to get the value in the Optional and return it from the method if a value is present. – Web User Apr 12 '18 at 23:40
  • 1
    @WebUser `input` is an `Optional`, `val` is a `String`. see this [Guide To Java 8 Optional](http://www.baeldung.com/java-optional) then see the java doc on the type [ifPresent](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html#ifPresent-java.util.function.Consumer-) returns. – Ousmane D. Apr 12 '18 at 23:41
  • @Aominè point taken, and appreciate the extra links you provided. – Web User Apr 13 '18 at 05:16

2 Answers2

6

All you have to do is change your return statement to:

return input.orElse("NOT_FOUND");

This will return the object in the Optional if present else returns "NOT_FOUND".

That said, I'd avoid using Optional's as parameters. see here.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • It's amazing how readable that is! But what if I wanted to manipulate the value in `input` before returning it? The equivalent of `return input.isPresent() ? input.get().substring(0,7).toUpperCase() : "NOT_FOUND";` I tried `return input.map(val -> val.substring(0,7).toUpperCase()).orElse("NOT_FOUND");` and it appears to work. Is that an appropriate use of the API to achieve the results I am after, or can I do better? – Web User Apr 13 '18 at 05:33
  • 1
    @WebUser The latter is better as it's more readable (IMO) and yes that is the appropriate use of the API to achieve the results you're after. – Ousmane D. Apr 13 '18 at 08:02
  • Thanks for confirming! – Web User Apr 13 '18 at 14:18
1

If you need to manipulate the string value, if it is present before returning it, use map method:

public static String getValue(Optional<String> input) {
    return input.map(s -> s.substring(0,7).toUpperCase()).orElse("NOT_FOUND");
} 

If input is empty the method returns default value - "NOT_FOUND", otherwise capitalized part of the string is returned.

getValue(Optional.ofNullable(null));
$6 ==> "NOT_FOUND"

getValue(Optional.of("some long string"));
$7 ==> "SOME LO"
Anton Balaniuc
  • 10,889
  • 1
  • 35
  • 53