java8新特性:Optional容器

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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值