-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathOptionalImprovements.java
42 lines (34 loc) · 1.24 KB
/
OptionalImprovements.java
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
40
41
42
package java9;
import org.junit.Test;
import java.util.Optional;
/**
* With Java 9 Optional is more functional that ever, no longer need to use isPresent, and instead use ifPresent
* operator, also allow compensation.
*/
public class OptionalImprovements {
/**
* If the optional contains the value we can use that value in constantClass consumer function.
* Otherwise any action it wont take effect over the optional.
*/
@Test
public void ifPresent() {
checkOptional(Optional.empty());
checkOptional(Optional.of("Foo"));
}
/**
* If the optional contains the value we can use that value in constantClass consumer function.
* Otherwise we can just run constantClass runnable action.
*/
@Test
public void ifPresentOrElse() {
checkOptionalOrElse(Optional.empty());
checkOptionalOrElse(Optional.of("Foo"));
}
private void checkOptional(Optional<String> opt) {
opt.ifPresent(value -> System.out.println("My value:" + value));
}
private void checkOptionalOrElse(Optional<String> opt) {
opt.ifPresentOrElse(value -> System.out.println("My value:" + value),
() -> System.out.println("The option value is not provided"));
}
}