阿里巴巴Java开发手册-使用JDK8的Opional类来防止出现NPE问题

    /**
     * https://www.baeldung.com/java-optional
     */
    @Test
    public void optionalTest(){
        Peo peo = new Peo("weijie", 18);
        /**
         * of、ofNullable
         */

        //of 判断peo是否为空,如果不为空程序继续执行
        Optional _of = Optional.of(peo);
        //程序直接抛出NullException
//        Optional _ofNull = Optional.of(null);
        //ofNullable 不管值是否为空仍然执行
        Optional _ofNullable = Optional.ofNullable(null);
        //如果值不为NULL则返回true,否则,false
        System.out.println("_of: " + _of.isPresent());
        System.out.println("_ofNullable: " + _ofNullable.isPresent());


        /**
         * orElse、orElseGet、orElseThroe
         */
        //orElse 饿汉式,不管b中值是否为空仍然会加载到内存当中,高并发场景慎用
        Peo _orElse = _ofNullable.orElse(peo);
        System.out.println("_orElse: " + _orElse);
        //orElseGet 懒汉式,当b中值为null才加载到内存当中
        Peo _orElseGet = _ofNullable.orElseGet(() -> peo);
        System.out.println("_orElseGet:" +_orElseGet);
        //orElseThrow 当值不存在时候返回一个异常
        Peo _orElseThrow = _ofNullable.orElseThrow(() -> new IllegalArgumentException());
        System.out.println("orElseThrow: " + _orElseThrow);

        /**
         * filter
         */
        //判断值是否和另一个值相等,通常和isPresent()搭配使用
        Optional _filter = _of.filter(p -> p.age == 18);
        System.out.println("_filter: " + _filter.isPresent());
        Optional _filter2 = _of.filter(p -> p.age == 16);
        System.out.println("_filter: " + _filter2.isPresent());

        /**
         * map、flatMap
         */
        //map 如果optional中有值则传入函数体获取Optional特定值,否则返回空值
        Optional _map = _of.map(Peo::getAge);
        boolean _age = _map.filter(age -> age >= 15).filter(age -> age <= 24).isPresent();
        System.out.println("_age: " + _age);
        //flatMap 该方法作用和map一样,区别是方法返回值不同 People::getAge -> Optional类型
        People people = new People("weijie", 25);
        Optional _flatMap = Optional.ofNullable(people).flatMap(People::getAge);
        System.out.println("_flatMap: " + _flatMap.get());
    }
    class Peo{
        String name;
        Integer age;

        public Peo(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return "Peo{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

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

    class People{
        String name;
        Integer age;

        public People(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public Optional getName() {
            return Optional.ofNullable(name);
        }

        public void setName(String name) {
            this.name = name;
        }

        public Optional getAge() {
            return Optional.ofNullable(age);
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }

你可能感兴趣的:(Java,#,基础)