字符串对象数组集合非空判断

String 字符串

//直接用字符串调用方法  str.isEmpty()    如果为null则会抛出异常 
    
if (StringUtils.isEmpty(str)){// "" 和null均判断为空
	System.out.println("string为空");
}

对象

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为空");
}

你可能感兴趣的:(java)