-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp2.java
More file actions
39 lines (24 loc) · 911 Bytes
/
App2.java
File metadata and controls
39 lines (24 loc) · 911 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
29
30
31
32
33
34
35
36
37
38
39
package kr.springboot.examples.ex11;
import kr.springboot.examples.commons.Cart;
import kr.springboot.examples.commons.Product;
import java.util.NoSuchElementException;
import java.util.Optional;
/**
* Optional.orElse/ orElseXXX Are a Perfect Replacement for isPresent()-get() Pair in Lambdas
* Optional.orElse/orElseXXX는 람다에서 isPresent()-get() 쌍을 완벽하게 대체한다.
*/
public class App2 {
// Avoid
public void avoidExample() {
Optional<Cart> cart = Optional.empty();
Product product = new Product();
if (!cart.isPresent() || !cart.get().getItems().contains(product))
throw new NoSuchElementException();
}
// Prefer
public void preferExample() {
Optional<Cart> cart = Optional.empty();
Product product = new Product();
cart.filter(c -> c.getItems().contains(product)).orElseThrow();
}
}