-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionalTest.java
More file actions
72 lines (57 loc) · 2.1 KB
/
Copy pathOptionalTest.java
File metadata and controls
72 lines (57 loc) · 2.1 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright 2021 tu.cn All right reserved. This software is the
* confidential and proprietary information of tu.cn ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Tu.cn
*/
package com.ariix.java8.optional;
import com.ariix.java8.lambda.Employee;
import org.junit.Test;
import java.util.Optional;
/**
* Description:
*
* @Author: Administrator
* Created: 2021/8/16
**/
public class OptionalTest {
@Test
public void testOf() {
Optional<Integer> result = Optional.of(1);
System.out.println("是否有值" + result.isPresent());
}
@Test
public void testElse() {
Optional<Integer> result = Optional.empty();
System.out.println("是否有值" + result.isPresent() + ", 如果没有值默认为"+ result.orElse(18));
}
@Test
public void testOfNullable() {
//允许把控制方法容器里
Optional<Employee> itWorker = Optional.ofNullable(null);
System.out.println("是否有IT 部分员工参与" + itWorker.isPresent());
}
private Optional<Employee> getItWorkder() {
return Optional.ofNullable(null);
}
@Test
public void testOrEleseGet () {
Optional<Employee> itWorker = getItWorkder();
if (itWorker.isPresent()) {
System.out.println(itWorker.toString() + " joins");
} else {
Employee employee = itWorker.orElseGet(() -> new Employee("jed", 52, 9865.85));
System.out.println(employee.toString() + " joins ");
}
}
@Test
public void testFlatmap() {
Optional<String> employee = Optional.of(new Employee("jed", 52, 9865.85))
.flatMap(employee1 -> Optional.of(employee1.getName()));
System.out.println(employee.get());
Optional<Employee> optionalEmployee = Optional.empty();
Optional<String> mvpName = optionalEmployee.flatMap(employee1 -> Optional.of(employee1.getName()));
System.out.println(mvpName.orElse("No One is MVP"));
}
}