Created
September 24, 2017 12:08
-
-
Save mdstoy/644be264dfc103ef3ba851fb8d7cac9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result: