Tagged: optional

orElse, or orElseGet, that is the question:

static String B() {
    return "B";

public static void main(final String... args) {
    System.out.println(Optional.of("A").orElseGet(() -> B()));

changing a nullable milliseconds to a nullable date using optional.

public Date toDate(@Nullable Long millis) {
    //return millis == null ? null : new Date(millis);
    return Optional.ofNullable(millis).map(Date::new).orElse(null);

Returning an Optional rather than a nullable Date instance is a better idea.

public Optional<Date> toDate(@Nullable Long millis) {
    return Optional.ofNullable(millis).map(Date::new);