Created
January 11, 2016 08:57
-
-
Save theomega/1b4ad5a45101d09c2e49 to your computer and use it in GitHub Desktop.
This file contains 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
package com.company; | |
import java.util.Optional; | |
/** | |
* Created by dominik on 11/01/16. | |
*/ | |
public class OptionalTest { | |
public static void main(String[] args) { | |
// Very ugly | |
doSomething().map(s -> { | |
System.out.println(s); | |
return null; | |
}); | |
// Less ugly | |
doSomething().ifPresent(s -> System.out.println(s)); | |
} | |
public static Optional<String> doSomething() { | |
if (Math.random() > 0.5) { | |
return Optional.of("Good"); | |
} else { | |
return Optional.empty(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment