应用场景

判断字符串、包装类、集合、对象是否为空。

Hutool工具

字符串工具-StrUtil (hutool.cn)

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

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

使用StrUtil.isEmpty(str)StrUtil.isBlank(str)

/*测试Hutool工具类 StrUtil ObjectUtil*/String str1 = null; //这个判断为空肯定为trueString str2 = "null"; //这个判断为空肯定为falseString str3 = ""; //这个判断为空人们希望为trueString str4 = " "; //这是空白字符,对于isEmpty和isNull来说判断为trueSystem.out.println(StrUtil.isEmpty(str1)); //trueSystem.out.println(StrUtil.isEmpty(str2)); //falseSystem.out.println(StrUtil.isEmpty(str3)); //trueSystem.out.println(StrUtil.isEmpty(str4)); //falseSystem.out.println("**************************");System.out.println(StrUtil.isBlank(str1)); //trueSystem.out.println(StrUtil.isBlank(str2)); //falseSystem.out.println(StrUtil.isBlank(str3)); //trueSystem.out.println(StrUtil.isBlank(str4)); //true

总结:

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

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

// 对于包装类只有这两种情况,用哪个都可以Integer i1 = null;Integer i2 = 1;// 判断指定对象是否为空,支持:// 1. CharSequence// 2. Map// 3. Iterable// 4. Iterator// 5. ArraySystem.out.println(ObjectUtil.isEmpty(i1)); //trueSystem.out.println(ObjectUtil.isEmpty(i2)); //falseSystem.out.println("**************************");// 检查对象是否为null 判断标准为:// 1. == null// 2. equals(null)System.out.println(ObjectUtil.isNull(i1)); //trueSystem.out.println(ObjectUtil.isNull(i2)); //false

总结:对于包装类,用哪个都可以

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

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

总结:

这是ObjectUtil的isEmpty(obj)方法

public static boolean isEmpty(Object obj) {if (null == obj) {return true;}if (obj instanceof CharSequence) {return StrUtil.isEmpty((CharSequence) obj);} else if (obj instanceof Map) {return MapUtil.isEmpty((Map) obj);} else if (obj instanceof Iterable) {return IterUtil.isEmpty((Iterable) obj);} else if (obj instanceof Iterator) {return IterUtil.isEmpty((Iterator) obj);} else if (ArrayUtil.isArray(obj)) {return ArrayUtil.isEmpty(obj);}return false;}

对于ArrayList集合,该方法调用了ArrayUtil.isEmpty(obj),进行了length的判断。因此使用isEmpty判断集合为空。

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

新建一个Person类

@Data@NoArgsConstructor@AllArgsConstructorpublic class Person{private Integer id;private String name;}

测试

Person p1 =null;Person p2 = new Person();Person p3 = new Person(1,"张三");System.out.println(ObjectUtil.isEmpty(p1)); //trueSystem.out.println(ObjectUtil.isEmpty(p2)); //falseSystem.out.println(ObjectUtil.isEmpty(p3)); //falseSystem.out.println("**************************");// 检查对象是否为null 判断标准为:// 1. == null// 2. equals(null)System.out.println(ObjectUtil.isNull(p1)); //trueSystem.out.println(ObjectUtil.isNull(p2)); //falseSystem.out.println(ObjectUtil.isNull(p3)); //falseSystem.out.println("**************************");System.out.println(BeanUtil.isEmpty(p1)); //trueSystem.out.println(BeanUtil.isEmpty(p2)); //trueSystem.out.println(BeanUtil.isEmpty(p3)); //false

BeanUtil.isEmpty(obj)总结:

判断Bean是否为空对象,空对象表示本身为null或者所有属性都为null 此方法不判断static属性

Params: bean – Bean对象
ignoreFieldNames – 忽略检查的字段名

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