Last active
January 15, 2016 03:14
-
-
Save nobeans/90a5f643f86c6668e097 to your computer and use it in GitHub Desktop.
Java8のOptional
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
def opt = Optional.of("HOGE") | |
assert opt.get() == "HOGE" | |
assert opt.isPresent() | |
assert opt.map { it * 2 }.get() == "HOGEHOGE" | |
assert opt.flatMap { Optional.of(it * 2) }.get() == 'HOGEHOGE' | |
assert opt.map { null } == Optional.empty() | |
// Optional.of(null) // => NPE | |
def nullOpt = Optional.ofNullable(null) | |
assert nullOpt == Optional.empty() | |
assert nullOpt.isPresent() == false | |
assert nullOpt.orElse("FOO") == "FOO" | |
assert nullOpt.map { it * 2 } == Optional.empty() | |
assert nullOpt.flatMap { Optional.of(it * 2) } == Optional.empty() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment