-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
28 lines (22 loc) · 868 Bytes
/
App.java
File metadata and controls
28 lines (22 loc) · 868 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
28
package kr.springboot.examples.ex8;
import java.util.Optional;
/**
* Consume an Optional if it Is Present. Do Nothing if it Is Not Present. This Is a Job For Optional.ifPresent().
* Optional이 있으면 사용하고 존재하지 않으면 아무것도 하지 마라. 이건 Optional.ifPresent()를 위한 것이다.
*/
public class App {
/**
* TheOptional.ifPresent()is a good alternative forisPresent()-get()
* pair when you just need to consume the value. If no value is present then do nothing.
*/
// Avoid
public void avoidExample() {
Optional<String> status = Optional.empty();
if (status.isPresent()) System.out.println("Status: " + status.get());
}
// Prefer
public void preferExample() {
Optional<String> status = Optional.empty();
status.ifPresent(System.out::println);
}
}