Quantcast
Channel: Why should Java 8's Optional not be used in arguments - Stack Overflow
Viewing all articles
Browse latest Browse all 29

Answer by Marko for Why should Java 8's Optional not be used in arguments

$
0
0

In case you want to have an Optional argument in your function rather do it using another functional interface. For example:

void doSomething(Supplier<Optional<String>> urlSupplier) {    urlSupplier.get()                .filter(StringUtils::isNotBlank)                .isPresent((url) -> System.out.println(url));}

You can use Function instead of Supplier it is up to you. Then you can just call it. For example:

void myMethod() {    doSomething(() -> methodToGetUrlAsOptional());}

Viewing all articles
Browse latest Browse all 29

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>