Java中的判断校验非空问题

目录

字符串

字符串是空的情况

字符串不是空的情况

对象

对象是空的情况

对象不是空的情况 

前端传的 int ,double类型等等

Optional 判断情况

https://www.cnblogs.com/zhangboyu/p/7580262.htmlhttps://www.cnblogs.com/zhangboyu/p/7580262.html

值为空的情况,不会进入lambda表达式

值不为空的情况


字符串

参考文档:工具类Apache Commons Lang3 之StringUtils-CSDN博客文章浏览阅读6.8k次,点赞5次,收藏9次。概述Lang3提供了许多Java库无法提供的辅助工具类,比如字符串,数值,反射,序列化等。如果熟练使用该包下的工具类,将节省我们的开发时间,避免重复实现,减少出错的可能。版本当前版本:3.12.03.x版本最低支持java7使用org.apache.commonscommons-lang3&l_stringutilshttps://blog.csdn.net/qq_42778001/article/details/124164478

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version> <!-- 请检查最新版本 -->
        </dependency>

依赖参考API文档

 common-lang3icon-default.png?t=N7T8https://hcdtc.github.io/zh/docs/30-development-manual/2-back-end/99-dev-utils/2-common-lang3/

字符串是空的情况

字符传 'a' 是否为空 ,  a=null , a="",时为空,"            " 不为空

String a = "";
StringUtils.isEmpty(a)     // 判断是否为空

 org.springframework.util.StringUtils.

字符串’a‘是否为空,a=null, a="”时,a="    ",为空 ,  “      a”不为空

String a = null;
StringUtils.isBlank("   a")

import org.apache.commons.lang3.StringUtils;

字符串不是空的情况

字符串’a‘是否为空,a=null, a="”时,a="    ",为空 ,  “      a”不为空

String a = 'b'
StringUtils.isNotBlank(a)

import org.apache.commons.lang3.StringUtil

集合 & Map

集合是空的情况


        List<Integer> integers = new ArrayList<>();
        
        if (CollectionUtils.isEmpty(integers)){
            System.out.println("是空");
        } else{
            System.out.println("空");
        }

org.springfremark.util

集合不是空的情况


        List<Integer> integers = new ArrayList<>();
        
        if (!CollectionUtils.isEmpty(integers)){
            System.out.println("是空");
        } else{
            System.out.println("空");
        }

org.springfremark.util

对象

对象是空的情况

        student = null;
        if (Objects.isNull(student)) {
            System.out.println("空");
        } else {
            System.out.println("不空");
        }


Java.Util

对象不是空的情况 

Student a = new Student;
a = null;
Objects.nonNull(a)

java.util

前端传的 int ,double类型等等

Integer num = ...; // 从前端获取的值

if (Objects.nonNull(num)) {
    System.out.println("num 不为空,其值为:" + num);
} else {
    System.out.println("num 为空");
}

Optional 判断情况

参考文章:

https://www.cnblogs.com/zhangboyu/p/7580262.htmlicon-default.png?t=N7T8https://www.cnblogs.com/zhangboyu/p/7580262.html

值为空的情况,不会进入lambda表达式

创建一个 Student 对象

Student student = new Student();

 将 student 设置为 null

student = null;

使用 Optional.ofNullable 创建 Optional 对象,用于包装 student

Optional<Student> opt = Optional.ofNullable(student);

如果 opt 中包含值,则执行 Lambda 表达式中的代码

opt.ifPresent(student1 -> {
    在这个 Lambda 表达式中执行一些操作,但由于 student 是 null,所以这部分代码不会执行
    System.out.println(student1.toString());
});

值不为空的情况

创建一个 Student 对象

Student student = new Student();

将 student 设置为 null

student = null;

 使用 Optional.ofNullable 创建 Optional 对象,用于包装 student

Optional<Student> opt = Optional.ofNullable(student);

如果 opt 中的值不为空,则返回该值;否则,使用 orElseGet 方法中的 Supplier 创建一个默认的 Student 对象

Student defaultStudent = opt.orElseGet(() -> {

     创建默认的 Student 对象

    Student defaultStu = new Student();

    对默认的 Student 对象设置一些默认值

    defaultStu.setGreen("defaultGreen");

    defaultStu.setRed("defaultRed");

     返回默认的 Student 对象
    return defaultStu;
});

输出默认的 Student 对象

System.out.println(defaultStudent);

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值