java8在util包下新增了Optional容器类。
Optional是一种包含或不包含空的对象的容器对象,当对象存在时,调用isPresent()返回true并且get()返回值。
orElse()依赖isPresent()的结果,存在返回结果,不存在使用新值
这是一个基于值得类,当使用标记敏感的操作,包括引用相等,hash值,同步,可能有不可预知的结果,应该避免。
常用方法、
Optional.of(T t) : 创建一个 Optional 实例
Optional.empty() : 创建一个空的 Optional 实例
Optional.ofNullable(T t):若 t 不为 null,创建 Optional 实例,否则创建空实例
isPresent() : 判断是否包含值
orElse(T t) : 如果调用对象包含值,返回该值,否则返回t
orElseGet(Supplier s) :如果调用对象包含值,返回该值,否则返回 s 获取的值
map(Function f): 如果有值对其处理,并返回处理后的Optional,否则返回 Optional.empty()
flatMap(Function mapper):与 map 类似,要求返回值必须是Optional
@Test
public void test1() {
Optional<Student> optional = Optional.of(new Student());
System.out.println(optional.get());
}
@Test
public void test2() {
Optional<Student> op1 = Optional.ofNullable(null);
System.out.println(op1.get());
Optional<Student> op2 = Optional.empty();
System.out.println(op2.get());
}
@Test
public void test3() {
Optional<Student> op1 = Optional.ofNullable(null);
if (op1.isPresent()) {
System.out.println(op1.get());
}
Student student = op1.orElse(new Student("cch", 15, 99, Student.Status.BUSY));
System.out.println(student);
Student student1 = op1.orElseGet(() -> new Student());
System.out.println(student1);
}
@Test
public void test4() {
Optional<Student> op1 = Optional.ofNullable(new Student("cch", 15, 99, Student.Status.BUSY));
Optional<Integer> op2 = op1.map(Student::getAge);
System.out.println(op2.get());
Optional<String> op3 = op1.flatMap((e) -> Optional.of(e.getName()));
System.out.println(op3.get());
}
public String getTeacherName(Student student) {
if (student != null) {
Teacher teacher = student.getTeacher();
if (teacher != null) {
return teacher.getName();
}
}
return "苍老师";
}
@Test
public void test5() {
Student student = new Student();
String teacherName = getTeacherName(student);
System.out.println(teacherName);
}
public String getTeacherName2(Optional<NewStudent> optional) {
return optional.orElse(new NewStudent())
.getTeacherOpt()
.orElse(new Teacher("苍老师"))
.getName();
}
@Test
public void test6() {
Optional<Teacher> teacherOptional = Optional.ofNullable(null);
NewStudent student = new NewStudent(teacherOptional);
Optional<NewStudent> op = Optional.ofNullable(student);
String teacherName = getTeacherName2(op);
System.out.println(teacherName);
}
public class NewStudent {
private Optional<Teacher> teacherOpt;
public Optional<Teacher> getTeacherOpt() {
return teacherOpt;
}
public void setTeacherOpt(Optional<Teacher> teacherOpt) {
this.teacherOpt = teacherOpt;
}
public NewStudent(Optional<Teacher> teacherOpt) {
this.teacherOpt = teacherOpt;
}
public NewStudent() {
}
}
public class Teacher {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher() {
}
public Teacher(String name) {
this.name = name;
}
}