Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package jp.co.confrage;
import java.util.Objects;
import java.util.Optional;
public class OptionalSample {
public static void main(String[] args) {
Integer max = Integer.valueOf(1);
if (Objects.isNull(max)) {
max = Integer.valueOf(2);
}
System.out.println(max); // 1
// ↓ if文消す
Optional<Integer> maxtemp = Optional.ofNullable(Integer.valueOf(1));
max = maxtemp.orElse(Integer.valueOf(2));
System.out.println(max); // 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment