import java.util.Objects;
// 员工信息
public class Employee {
private String name; // 姓名
private Integer age; // 年龄
private Double salary; // 工资
private Status status; // 状态
public enum Status {
FREE,
BUSY,
VOCATION;
}
public Employee() {}
public Employee(String name, Integer age, Double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(String name, Integer age, Double salary, Status status) {
this.name = name;
this.age = age;
this.salary = salary;
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", status=" + status +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(name, employee.name) &&
Objects.equals(age, employee.age) &&
Objects.equals(salary, employee.salary);
}
@Override
public int hashCode() {
return Objects.hash(name, age, salary);
}
}
import org.junit.Test;
import java.util.Optional;
/**
* Optional常用方法测试:
Optional.of(T t): 创建一个Optional实例
Optional.empty(): 创建一个空的Optional实例
Optional.ofNullable(T t): 若t 不为null, 创建Optional实例,否则创建空实例
isPresent(): 判断是否包含值
orElse(T t): 如果调用对象包含值,返回该值, 否则返回t.
orElseGet: 如果调用对象包含值, 返回该值, 否则返回s获取的值
map: 如果有值对其处理, 并返回处理后的Optional, 否则返回Optional.empty()
flatMap: 与map类似, 要求返回值必须是Optional
*/
public class OptionalTest {
@Test
public void demo1() {
System.out.println("------ 创建一个Optional实例 ----------");
Optional op1 = Optional.of(new Employee());
System.out.println(op1.get());
System.out.println("----- 创建一个空的Optional实例 ------");
Optional op2 = Optional.empty();
System.out.println(op2.isPresent()? "op2的值不为空": "op2的值为空");
System.out.println("-- 若t 不为null, 创建Optional实例,否则创建空实例 ------");
Optional op3 = op2.ofNullable(new Employee("张三", 12, 2222.22, Employee.Status.BUSY));
Optional op31 = Optional.ofNullable(new Employee("张三", 12, 2222.22, Employee.Status.BUSY));
System.out.println("op3: " + op3.get());
System.out.println("op31: " + op31.get());
System.out.println("--------------- isPresent:判断是否包含值 ---------------");
if (op2.isPresent()) {
System.out.println("op2不为空");
} else {
System.out.println("op2为空");
}
if (op3.isPresent()) {
System.out.println("op3不为空");
} else {
System.out.println("op3为空");
}
System.out.println("--------- 如果调用对象包含值,返回该值, 否则返回t. ----------");
Employee emp1 = op2.orElse(new Employee("李四", 13, 3333.33, Employee.Status.FREE));
System.out.println("emp1: " + emp1);
Employee emp2 = op3.orElse(new Employee("李四", 13, 3333.33, Employee.Status.FREE));
System.out.println("emp2: " + emp2);
System.out.println("------ 如果调用对象包含值, 返回该值, 否则返回s获取的值 ------");
op2.orElseGet(() -> new Employee("王五", 19, 444.44, Employee.Status.VOCATION));
System.out.println("--- 如果有值对其处理, 并返回处理后的Optional, 否则返回Optional.empty() ---");
Optional op4 = op2.map((e) -> e.getName());
Optional op5 = op3.map((e) -> e.getName());
System.out.println("op4: " + op4);
System.out.println("op5: " + op5);
System.out.println("------ 与map类似, 要求返回值必须是Optional -----");
Optional op6 = op2.flatMap(e -> Optional.of(e.getName()));
Optional op7 = op3.flatMap(e -> Optional.of(e.getName()));
System.out.println("op6: " + op6);
System.out.println("op7: " + op7);
}
}