String 字符串
import org.apache.commons.lang3.StringUtils;
//直接用字符串调用方法 str.isEmpty() 如果为null则会抛出异常
if (StringUtils.isEmpty(str)){// "" 和null均判断为空
System.out.println("string为空");
}
maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
对象
import java.util.Objects;
if (Objects.isNull(obj)){ //null
System.out.println("obj为空");
}
数组
//数组需要判断null和长度length(大小size)
if (arr == null || arr.length == 0){
System.out.println("arr为空");
}
list
set用法同list
//isEmpty()方法内部就是判断size大小
if (list.isEmpty() && list == null){
System.out.println("list为空");
}
//使用spring提供的工具类
if (CollectionUtils.isEmpty(list)){
System.out.println("list为空");
}
maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>