-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
27 lines (22 loc) · 874 Bytes
/
App.java
File metadata and controls
27 lines (22 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package kr.springboot.examples.ex9;
import java.util.Optional;
/**
* Consume an Optional if it Is Present. If it Is Not Present, Then Execute an Empty-Based Action. This Is a Job For Optional.ifPresentElse(), Java 9.
* Optional이 있으면 사용하고 존재하지 않으면 Empty 기반 작업을 해라. 이것은 Optional.ifPresentElse() / Java 9 이상
*/
public class App {
// Avoid
public void avoidExample() {
Optional<String> status = Optional.empty();
if (status.isPresent()) System.out.println("Status: " + status.get());
else System.out.println("Status not found");
}
// Prefer
public void preferExample() {
Optional<String> status = Optional.empty();
status.ifPresentOrElse(
System.out::println,
() -> System.out.println("Status not found")
);
}
}