Skip to content

Instantly share code, notes, and snippets.

@mdstoy
Created September 24, 2017 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdstoy/644be264dfc103ef3ba851fb8d7cac9a to your computer and use it in GitHub Desktop.
Save mdstoy/644be264dfc103ef3ba851fb8d7cac9a to your computer and use it in GitHub Desktop.
import java.util.Optional;
public class Java9OptionalTrial {
public static void main(String[] args) {
new Java9OptionalTrial().ifPresentTrial();
}
public void ifPresentTrial() {
present().ifPresentOrElse(
s -> System.out.println(s),
() -> System.out.println("not present")
);
notPresent().ifPresentOrElse(
s -> System.out.println(s),
() -> System.out.println("not present")
);
}
private Optional<String> present() {
return Optional.of("present");
}
private Optional<String> notPresent() {
return Optional.empty();
}
}
@mdstoy
Copy link
Author

mdstoy commented Sep 24, 2017

Result:

$ java Java9OptionalTrial
present
not present
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment