Skip to content

Instantly share code, notes, and snippets.

@theomega
Created January 11, 2016 08:57
Show Gist options
  • Save theomega/1b4ad5a45101d09c2e49 to your computer and use it in GitHub Desktop.
Save theomega/1b4ad5a45101d09c2e49 to your computer and use it in GitHub Desktop.
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