forked from whiteship/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Optional 소개
Jeonghyun Kang edited this page May 23, 2022
·
3 revisions
public Progress getProgress() {
return progress;
}
Progress progress = spring_boot.getProgress();
if (progress != null) {
System.out.println("Not null");
}
public Progress getProgress() {
if (this.progress == null) {
throw new IllegalStateException();
}
return progress;
}
public Optional<Progress> getProgress() {
return Optional.ofNullable(progress);
}
Optional<Progress> progress = spring_boot.getProgress();
if (progress.isPresent()) {
System.out.println("Not null");
}
primitive type용 Optional이 따로 있다. (성능이 더 좋음)
Optional.of(10); // (X)
OptionalInt.of(10); // (O)
- 절대 null을 return하면 안된다.
public Optional<T> getItem() {
return null
-
Optional.empty()
를 return 한다.
public Optional<T> getItem() {
return Optional.empty();
그 자체로 비어있는지 아닌지 확인이 가능하다.
- Collection
- Map
- Stream Array
- Optional