java中各种判空
//字符串
String str="";
Strings.isBlank(str);//true
//实体类
RegisterManInfo registerManInfo=new RegisterManInfo();
CommonFunction.objCheckIsNull(registerManInfo);//使用反射工具类 判断 此处为false
CommonFunction.objCheckIsNull(registerManInfo,Arrays.asList("serialVersionUID"),null);//剔除 赋值的那条 此处为true
//集合
List list=new ArrayList();
CollectionUtils.isEmpty(list);//true
//map集合
Map<String,Object> map=new HashMap<>();
MapUtil.isEmpty(map);//true
//数组
Integer[] num=new Integer[]{};
ArrayUtil.isEmpty(num);//true
//json
Map<String,Object> returnContent=new HashMap<>();
returnContent.put("msg",null);
returnContent.put("code","200");
returnContent.put("data","成功");
if(MapUtil.isNotEmpty(returnContent)) {//不为null
JSONObject jsonObject=new JSONObject(returnContent);
for (String key : jsonObject.keySet()) {
if(key.equals("msg")){
Object msg = jsonObject.get("msg");//获取值
if(msg==null){
System.out.println("msg的值为null,正确判空");
}else{
System.out.println(msg);
}
}
}
}
实例化之后实体类的判空 工具类
package com.dykj.register.ao;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;
/**
* 来源
* https://blog.csdn.net/qq_35566813/article/details/90914062?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168681785016782427444350%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168681785016782427444350&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-7-90914062-null-null.142^v88^control_2,239^v2^insert_chatgpt&utm_term=java%E5%88%A4%E6%96%AD%E5%AF%B9%E8%B1%A1%E4%B8%8D%E4%B8%BAnull&spm=1018.2226.3001.4187
* */
public class CommonFunction {
/**
* description:定义一个System.out.println的开(纯属个人习惯)
**/
private static int objectNullSystemOutFlag = 0;
/**
* description:判断当前对象是否为空(包括所有属性为空)
*
* @author ZhangXihui
* @param object 入参类
* @return boolean
* @create 2019/6/3 17:34
**/
public static boolean objCheckIsNull(Object object) {
if (object == null) {
return true;
}
// 得到类对象
Class clazz = object.getClass();
// 得到所有属性
Field[] fields = clazz.getDeclaredFields();
//定义返回结果,默认为true
boolean flag = true;
for (Field field : fields) {
//设置权限(很重要,否则获取不到private的属性,不了解的同学补习一下反射知识)
field.setAccessible(true);
Object fieldValue = null;
String fieldName = null;
try {
//得到属性值
fieldValue = field.get(object);
//得到属性类型
Type fieldType = field.getGenericType();
//得到属性名
fieldName = field.getName();
//打印输出(调试用可忽略)
if (objectNullSystemOutFlag == 1) {
System.out.println("属性类型:" + fieldType + ",属性名:" + fieldName + ",属性值:" + fieldValue);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
//只要有一个属性值不为null 就返回false 表示对象不为null ;实体类实现序列化接口的时候避免返回false作用
if (fieldValue != null) {
flag = false;
break;
}
}
return flag;
}
/**
* description:判断当前对象是否为空(包括所有属性为空)
* 可选则在判断规则中剔除某一字段,或者某一值
*
* @author ZhangXihui
* @param object 入参对象
* @param excludeNameList 要剔除的属性名称,没有就传空集合或者null
* @param excludeValueList 要剔除的数值,没有就传空集合或者null
* @return boolean
* @create 2019/6/3 17:34
**/
public static boolean objCheckIsNull(Object object, List<String> excludeNameList, List<Object> excludeValueList) {
if (object == null) {
return true;
}
// 得到类对象
Class clazz = object.getClass();
// 得到所有属性
Field[] fields = clazz.getDeclaredFields();
//判断入参
boolean excludeNameListFlag = false;
if (excludeNameList != null && excludeNameList.size() > 0) {
excludeNameListFlag = true;
}
boolean excludeValueListFlag = false;
if (excludeValueList != null && excludeValueList.size() > 0) {
excludeValueListFlag = true;
}
//定义返回结果,默认为true
boolean flag = true;
for (Field field : fields) {
field.setAccessible(true);
Object fieldValue = null;
String fieldName = null;
try {
//得到属性值
fieldValue = field.get(object);
//得到属性类型
Type fieldType = field.getGenericType();
//得到属性名
fieldName = field.getName();
//剔除指定属性名的属性值
if (excludeNameListFlag) {
for (String s : excludeNameList) {
if (fieldName.equals(s)) {
fieldValue = null;
break;
}
}
}
//剔除指定属性值
if (excludeValueListFlag) {
for (Object obj : excludeValueList) {
if (obj.equals(fieldValue)) {
fieldValue = null;
break;
}
}
}
//打印输出(调试用可忽略)
if (objectNullSystemOutFlag == 1) {
System.out.println("属性类型:" + fieldType + ",属性名:" + fieldName + ",属性值:" + fieldValue);
}
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
//只要有一个属性值不为null 就返回false 表示对象不为null 忽略序列化
if (fieldValue != null && !"serialVersionUID".equals(fieldName)) {
flag = false;
break;
}
}
//打印输出(调试用可忽略)
if (objectNullSystemOutFlag == 1) {
System.out.println("忽略属性: " + excludeNameList + " 忽略值: " + excludeValueList);
}
return flag;
}
}