侧边栏壁纸
博主头像
程序员Jaye博主等级

但行好事,莫问前程

  • 累计撰写 15 篇文章
  • 累计创建 12 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Java8 optional对象

codejaye
2023-04-14 / 0 评论 / 0 点赞 / 64 阅读 / 7107 字

Java8 optional对象

Java8 之Optional类

empty()

   /**
    * 创建一个空的Optional
    *
    * @see read.java.util.Optional#empty()
    * @see java.util.Optional#empty()
    */
   @Test
   void empty() {
       Optional<Object> optionalEmpty = Optional.empty();
       System.out.println(optionalEmpty.toString());
   }

of()

   /**
    * 创建一个Optional类,传入对象不能为空,否则会抛出空指针异常
    *
    * @see read.java.util.Optional#of(Object)
    * @see java.util.Optional#of(Object)
    */
   @Test
   void of() {
       //Optional<Object> optional = Optional.of(null);
       Optional<Object> optional = Optional.of(1);
       Object o = optional.get();
       log.info("===1===");
   }

ofNullable()

   /**
    * 创建一个Optional类,传入对象为空时候返回一个Optional.empty
    *
    * @see read.java.util.Optional#ofNullable(Object)
    * @see java.util.Optional#ofNullable(Object)
    */
   @Test
   void ofNullable() {
       Optional<Object> optionalNull = Optional.ofNullable(null);
       System.out.println("optionalNull = " + optionalNull);
   }

get()

    /**
     * 获取Optional内存放的对象
     *
     * @see read.java.util.Optional#get()
     * @see java.util.Optional#get()
     */
    @Test
    void get() {
        Map<Object, Object> map = MapUtil.builder().put("1", 1).put("2", 2).build();
        Optional<Map<Object, Object>> optionalMap = Optional.ofNullable(map);
        Map<Object, Object> getMap = optionalMap.get();
        MapBuilder<Object, Object> put = MapUtil.builder().putAll(getMap).put("3", 3);
        Assert.equals(map, getMap);
        //Assert.equals(map, put);
    }

isPresent()

    /**
     * 判断包装器实例中真实对象是否存在
     *
     * @see read.java.util.Optional#isPresent()
     * @see java.util.Optional#isPresent()
     */
    @Test
    void isPresent() {
        Map<Object, Object> map = MapUtil.builder().put("1", 1).put("2", 2).build();
        Optional<Map<Object, Object>> optionalMap = Optional.ofNullable(map);
        boolean present = optionalMap.isPresent();
        System.out.println(present);
        Optional<Object> emptyOptional = Optional.empty();
        boolean emptyOptionalPresent = emptyOptional.isPresent();
        System.out.println(emptyOptionalPresent);

    }

ifPresent()

    /**
     * <p>如果包包裹的对象存在 则执行回调方法</p>
     *
     * <p>高频使用方法</p>
     *
     * @see read.java.util.Optional#ifPresent(Consumer)
     * @see java.util.Optional#ifPresent(Consumer)
     */
    @Test
    void ifPresent() {
        Map<Object, Object> map = MapUtil.builder().put("1", 1).put("2", 2).build();
        Optional<Map<Object, Object>> optionalMap = Optional.ofNullable(map);
        optionalMap.ifPresent(m -> m.put("3", 3));
        System.out.println("map = " + map);
        Map nullMap = null;
        Optional<Map> nullOptionalMap = Optional.ofNullable(nullMap);
        nullOptionalMap.ifPresent(System.out::println);
    }

filter()

    /**
     * 根据条件过滤掉无用数据
     *
     * @see read.java.util.Optional#filter(Predicate)
     * @see java.util.Optional#filter(Predicate)
     */
    @Test
    void filter() {
        Optional.of(new Student()).filter(student -> Objects.equals(student.getAge(), "18")).ifPresent(Student::getAge);
    }

map()

    /**
     * 将被包裹的对象从{@code T}实例转化成{@code U}实例
     * <pre>
     *     Long userId = Optional.ofNullable(user).map(User::getUserId).orElse(null);
     * </pre>
     * <p>传统方式只能按照如下方式编码</p>
     * <pre>
     *     if(user != null){
     *         Long userId = user.getUserId();
     *     }
     * </pre>
     *
     * <p>高频使用方法</p>
     *
     * @see read.java.util.Optional#map(Function)
     * @see java.util.Optional#map(Function)
     */
    @Test
    void map() {
        HashMap<Object, Object> hashMap = new HashMap<>();
        Optional<HashMap<Object, Object>> optionalMap = Optional.ofNullable(hashMap);
        Student student = new Student("1", "Name", "18");
        Optional<Student> optionalStudent = Optional.ofNullable(student);
        Optional<Optional<Optional<String>>> s = optionalStudent.map(this::getStudentName);

    }

flatmap()

    /**
     * map 和flatMap 的区别存在一个解包的过程解开一层
     *
     * @see read.java.util.Optional#flatMap(Function)
     */
    @Test
    void flatMap() {
        Student student = new Student("1", "ame", "18");
        Optional<Student> optionalStudent = Optional.ofNullable(student);
        Optional<Optional<String>> s = optionalStudent.flatMap(this::getStudentName);
    }

orElse()

    /**
     * orElse和orElseGet区别,orElse是饿汉式,无论是不是都执行,orElseGet是懒汉式,假如不满足就不执行了
     *
     * @see read.java.util.Optional#orElse(Object)
     */
    @Test
    void orElse() {
        //Student student =null;
        Student student = new Student("1", "ame", "18");
        Student student1 = Optional.ofNullable(student).orElse(createStudent());
    }

orElseGet()

    /**
     * orElse和orElseGet区别,orElse是饿汉式,无论是不是都执行,orElseGet是懒汉式,假如不满足就不执行了
     *
     * @see read.java.util.Optional#orElseGet(Supplier)
     */
    @Test
    void orElseGet() {
        Student student = null;
        //Student student = new Student("1", "ame","18");
        Student student1 = Optional.ofNullable(student).orElseGet(this::createStudent);
    }

orElseThrow()

    /**
     * 不满足抛出异常
     * @see read.java.util.Optional#orElseThrow(Supplier)
     * @see java.util.Optional#orElseGet(Supplier)
     */
    @Test
    void orElseThrow() throws Exception {
        //Student student = null;
        Student student = new Student("1", "ame","18");
        Student student1 = Optional.ofNullable(student).orElseThrow(Exception::new);
    }

参考教程

0

评论区