java判断list不为空的实现,和限制条数不要在一起写
作者:chushiyunen 时间:2022-01-24 13:14:38
场景
很多情况下,查单条记录也用通用查询接口,但是输入的条件却能确定唯一性。如果我们要确定list中只有一条记录,如下写法:
// 记录不为空 && 只有一条 才继续
if(!CollectionUtils.isEmpty(list) && 1!=list.size()){
return "记录条数不是1";
}
Object object = list.get(0);
上面代码对么,貌似正确啊。后来报错了,被打脸了。
其实相当于 >0 && !=1 恰好漏掉了 =0 这种情况,
因此get(0)完美报错。
解决方案
像这种条件不要怕麻烦,多写几个if更清晰。
补充:判断一个java对象中的属性值是否为空(批量判断)
有时候数据库中的某些字段值要求不为空,所以代码中要判断这些字段对应的属性值是否为空,当对象属性过多时,一个一个属性去判断,会显得代码冗余,所以,可以借助工具类
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.FatalBeanException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class IsNull {
//整个类都校验
public static List<String> validateProperty(Object validateObj) {
return validateProperty(validateObj,(String[])null);
}
//类中的某些字段不校验
public static List<String> validateProperty(Object validateObj,String... ignoreProperties) {
PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(validateObj.getClass());
List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null);
List<String> errList = new ArrayList<>();
for (PropertyDescriptor targetPd : targetPds) {
Method readMethod = targetPd.getReadMethod();
if (readMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) {
try {
if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
readMethod.setAccessible(true);
}
Object value = readMethod.invoke(validateObj);
if (value instanceof String) {
if (StringUtils.isEmpty((String) value)) {
errList.add(validateObj.getClass().getSimpleName()+ "中的" + targetPd.getName() + "不能为空");
continue;
}
}
if (value instanceof Float || value instanceof Integer) {
if (StringUtils.isEmpty(value.toString())) {
errList.add(validateObj.getClass().getSimpleName()+ "中的" + targetPd.getName() + "不能为空");
continue;
}
}
if (value == null) {
errList.add(validateObj.getClass().getSimpleName() + "中的" + targetPd.getName() + "不能为空");
}
} catch (Throwable ex) {
throw new FatalBeanException(
"Could not copy property '" + targetPd.getName() + "' from source to target", ex);
}
}
}
return errList;
}
}
之后对拿到的数据进行业务判断
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/enthan809882/article/details/105589154
标签:java,判断,不为空,list
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于java构造方法Vector修改元素源码分析
2023-11-25 10:54:56
![](https://img.aspxhome.com/file/2023/7/60237_0s.png)
迅速掌握Java容器中常用的ArrayList类与Vector类用法
2022-12-16 16:18:47
使用Jitpack发布开源Java库的详细流程
2021-12-18 06:55:58
![](https://img.aspxhome.com/file/2023/5/63395_0s.png)
Go Java算法之K个重复字符最长子串详解
2022-02-10 17:53:29
C#简单爬虫案例分享
2023-04-14 08:44:13
Java内存模型final的内存语义
2023-06-05 08:02:25
![](https://img.aspxhome.com/file/2023/0/62130_0s.png)
java实现实时通信聊天程序
2023-10-18 09:08:06
![](https://img.aspxhome.com/file/2023/3/70583_0s.jpg)
java实战之飞机大战小游戏(源码加注释)
2021-08-01 17:46:08
![](https://img.aspxhome.com/file/2023/5/63485_0s.png)
详解Spring ApplicationContext加载过程
2023-07-17 01:07:43
SrpingDruid数据源加密数据库密码的示例代码
2021-06-21 03:26:26
![](https://img.aspxhome.com/file/2023/1/63021_0s.png)
JAVA十大排序算法之快速排序详解
2022-06-08 16:09:11
![](https://img.aspxhome.com/file/2023/5/69315_0s.png)
JavaWeb 中Cookie实现记住密码的功能示例
2023-04-06 05:06:48
![](https://img.aspxhome.com/file/2023/2/69102_0s.png)
详解tryAcquire()、addWaiter()、acquireQueued()
2022-07-30 10:24:05
Java实现图片倒影的源码实例内容
2022-08-30 02:39:24
![](https://img.aspxhome.com/file/2023/2/65162_0s.jpg)
Java线程的生命周期命名与获取代码实现
2022-05-21 05:19:49
![](https://img.aspxhome.com/file/2023/5/64145_0s.jpg)
spring cloud gateway请求跨域问题解决方案
2021-11-05 11:19:25
MyBatis框架迭代器模式实现原理解析
2021-08-07 13:56:00
c#实现爬虫程序
2023-04-19 18:59:14
![](https://img.aspxhome.com/file/2023/8/72478_0s.jpg)
基于java配置nginx获取真实IP代码实例
2023-07-15 12:32:46
Servlet+JDBC实现登陆功能的小例子(带验证码)
2021-05-29 03:04:25
![](https://img.aspxhome.com/file/2023/7/69897_0s.png)