Skip to content

Instantly share code, notes, and snippets.

@bugabinga
Created March 28, 2014 14:19
Show Gist options
  • Save bugabinga/9833966 to your computer and use it in GitHub Desktop.
Save bugabinga/9833966 to your computer and use it in GitHub Desktop.
Java Optional type example
class MaybeBaby {
public final Optional<String> data;
public MaybeBaby(String data) {
this.data = Optional.of(data) ;
}
public MaybeBaby() {
data = Optional.empty() ;
}
public static main(String[] args) {
MaybeBaby no = new MaybeBaby() ;
MaybeBaby yes = new MaybeBaby("yes") ;
no.ifPresent(data -> System.out.println("will never run")) ;
// will print yes
yes.ifPresent(System.out::println) ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment