JAVA 8 Optional基本使用

一、什么是Optional

Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象。

Optional 是个容器:它可以保存类型T的值,或者仅仅保存null。Optional提供很多有用的方法,这样我们就不用显式进行空值检测。

Optional 类的引入很好的解决空指针异常。

常用方法

ofNullable(可以传递一个空对象)

Of(不可以传递空对象)

二、基本使用

2.1、判断参数是否为空

        String name = "coderWang";
        Optional optional = Optional.ofNullable(name);
        if (optional.isPresent()) {
            System.out.println("使用isPresent:" + optional.get());
        }
        Optional.ofNullable(name).ifPresent(s -> System.out.println("使用ifPresent:" + s));
         // 方法引用的方式
        Optional.ofNullable(name).ifPresent(System.out::println);

使用isPresent:coderWang
使用ifPresent:coderWang
coderWang

2.2、过滤

        String name ="coderWang";
        boolean present = Optional.ofNullable(name).filter(s -> "coderWang".equals(s)).isPresent();
        System.out.println(present);

true

2.3 、设置默认值(orElse / orElseGet)

  • Optonal为空时,无论orElse还是orElseGet都会执行getDefault()方法;
  • Optional有值时,orElse会执行,而orElseGet不会执行。

orElse 

         不能配合方法引用

 private static UserEntity userEntity;

    public static void main(String[] args) {
        UserEntity user = test.getUser();
        System.out.println("通过getUser:" + user);
        System.out.println("全局user:" + userEntity);
    }

    public static UserEntity getUser() {
        //         原始写法
//        if (userEntity == null) {
//            return createUser();
//        }
//        return userEntity;
//       1.8 写法
        return Optional.ofNullable(userEntity).orElse(orElseGet());
    }

    private static UserEntity orElseGet() {
        userEntity = createUser();
        return userEntity;
    }

    private static UserEntity createUser() {
        return new UserEntity(1, "coderWang");
    }

通过getUser:com.entry.entity.UserEntity@154617c
全局user:com.entry.entity.UserEntity@154617c

orElseGet

  private static UserEntity userEntity;

    public static void main(String[] args) {
        UserEntity user = test.getUser();
        System.out.println("通过getUser:" + user);
        System.out.println("全局user:" + userEntity);
    }

    public static UserEntity getUser() {
//         return Optional.ofNullable(userEntity).orElseGet(()->orElseGet());
        return Optional.ofNullable(userEntity).orElseGet(test::orElseGet);
    }

    private static UserEntity orElseGet() {
        userEntity = createUser();
        return userEntity;
    }

    private static UserEntity createUser() {
        return new UserEntity(1, "coderWang");
    }

 通过getUser:com.entry.entity.UserEntity@efb846
全局user:com.entry.entity.UserEntity@efb846

2.4、实例 (对实体某个字段做处理)


    public static void main(String[] args) {
        UserEntity user = new UserEntity(1, "coDerWanG");
        System.out.println("JAVA8之前处理" + toJDK7(user));
        System.out.println("JAVA8处理" + toJDK8(user));
    }

    private static String toJDK7(UserEntity user) {
        if (user != null) {
            String orderName = user.getName();
            if (orderName != null) {
                return orderName.toLowerCase();
            }
        }
        return null;
    }

    private static String toJDK8(UserEntity user) {
        return Optional.ofNullable(user).map(orderEntity -> orderEntity.getName()).
                map(name -> name.toUpperCase()).
                orElse(null);
    }

JAVA8之前处理coderwang
JAVA8处理CODERWANG

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值