利用Hutool工具类判断字符串和对象是否为空

首先导入依赖

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.20</version>
        </dependency>

1. 判断字符串是否为空(StrUtil的使用)

str1为null,str2是字符串,str3为空字符串,str4是包含不可见字符(空格等)的字符串。

isBlank与 isEmpty(CharSequence) 的区别是: isBlank方法会校验空白字符,且性能相对于 isEmpty(CharSequence) 略慢。

/*
    StrUtil ObjectUtil
*/
String str1 = null; 
String str2 = "null"; 
String str3 = ""; 
String str4 = " "; 

System.out.println(StrUtil.isEmpty(str1)); //true
System.out.println(StrUtil.isEmpty(str2)); //false
System.out.println(StrUtil.isEmpty(str3)); //true
System.out.println(StrUtil.isEmpty(str4)); //false
System.out.println("===================");
System.out.println(StrUtil.isBlank(str1)); //true
System.out.println(StrUtil.isBlank(str2)); //false
System.out.println(StrUtil.isBlank(str3)); //true
System.out.println(StrUtil.isBlank(str4)); //true

2.判断包装类是否为空(ObjectUtil的使用)


Integer i1 = null;
Integer i2 = 1;
// 判断指定对象是否为空,支持:
// 1. CharSequence
// 2. Map
// 3. Iterable
// 4. Iterator
// 5. Array
System.out.println(ObjectUtil.isEmpty(i1)); //true
System.out.println(ObjectUtil.isEmpty(i2)); //false
System.out.println("**************************");
// 检查对象是否为null 判断标准为:
// 1. == null
// 2. equals(null)
System.out.println(ObjectUtil.isNull(i1)); //true
System.out.println(ObjectUtil.isNull(i2)); //false

3.判断集合是否为空(ObjectUtil的使用)

ArrayList list1 = null; 
ArrayList<String> list2 = new ArrayList<>();
ArrayList<String> list3 = new ArrayList<>(List.of("list1","list2","list3")); //这个判断为空肯定为false
// 判断指定对象是否为空,支持:
// 1. CharSequence
// 2. Map
// 3. Iterable
// 4. Iterator
// 5. Array
System.out.println(ObjectUtil.isEmpty(list1)); //true
System.out.println(ObjectUtil.isEmpty(list2)); //true
System.out.println(ObjectUtil.isEmpty(list3)); //false
System.out.println("**************************");
// 检查对象是否为null 判断标准为:
// 1. == null
// 2. equals(null)
System.out.println(ObjectUtil.isNull(list1)); //true
System.out.println(ObjectUtil.isNull(list2)); //false
System.out.println(ObjectUtil.isNull(list3)); //false

4.判断对象是否为空(ObjectUtil的使用)

此方法不判断static属性
 

User p1 =null;
User p2 = new User ();
User p3 = new User (1,"a");

System.out.println(ObjectUtil.isEmpty(p1)); //true
System.out.println(ObjectUtil.isEmpty(p2)); //false
System.out.println(ObjectUtil.isEmpty(p3)); //false
System.out.println("**************************");
// 检查对象是否为null 判断标准为:
// 1. == null
// 2. equals(null)
System.out.println(ObjectUtil.isNull(p1)); //true
System.out.println(ObjectUtil.isNull(p2)); //false
System.out.println(ObjectUtil.isNull(p3)); //false
System.out.println("**************************");

System.out.println(BeanUtil.isEmpty(p1)); //true
System.out.println(BeanUtil.isEmpty(p2)); //true
System.out.println(BeanUtil.isEmpty(p3)); //false

Params: bean – Bean对象

ignoreFieldNames – 忽略检查的字段名

Returns:是否为空,true - 空 / false - 非空

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值