Java利用反射对list对象做过滤
目录
- 1.对象结构
- 2.根据字段名获取value
- 3.对list过滤
- 4.精确匹配
- 5.模糊匹配
文章图片
?反射对效率有影响 慎用!!!?
1.对象结构
public class BusinessDept {private String year; //年private String month; //月private String deptName; //部门名称private String deptLeader; //部门负责人private Double jyz; //经营值private Double rev; //收入private Double exp; //支出private Double tWorkTime; //填报工时private Double sWorkTime; //标准工时private Double param; //经营参数public void setYear(String year) {this.year = year; }public void setMonth(String month) {this.month = month; }public void setDeptName(String deptName) {this.deptName = deptName; }public void setDeptLeader(String deptLeader) {this.deptLeader = deptLeader; }public void setJyz(Double jyz) {this.jyz = jyz; }public String getYear() {return year; }public String getMonth() {return month; }public String getDeptName() {return deptName; }public String getDeptLeader() {return deptLeader; }public Double getJyz() {return jyz; }public Double getRev() {return rev; }public Double getExp() {return exp; }public Double gettWorkTime() {return tWorkTime; }public Double getsWorkTime() {return sWorkTime; }public Double getParam() {return param; }public void setRev(Double rev) {this.rev = rev; }public void setExp(Double exp) {this.exp = exp; }public void settWorkTime(Double tWorkTime) {this.tWorkTime = tWorkTime; }public void setsWorkTime(Double sWorkTime) {this.sWorkTime = sWorkTime; }public void setParam(Double param) {this.param = param; }publicE get(String name) {Class cl = this.getClass(); Field[] fields = cl.getDeclaredFields(); try {for (Field field : fields) {field.setAccessible(true); if (field.getName().equals(name)) {return (E) field.get(this); }}} catch (IllegalAccessException e) {e.printStackTrace(); }return null; }}
2.根据字段名获取value
publicE get(String name) {Class cl = this.getClass(); Field[] fields = cl.getDeclaredFields(); try {for (Field field : fields) {field.setAccessible(true); if (field.getName().equals(name)) {return (E) field.get(this); }}} catch (IllegalAccessException e) {e.printStackTrace(); }return null; }
3.对list过滤
List businessDeptList = this.myQuery(); //筛选符合条件的listif (businessDeptList.size()>0){businessDeptList = this.filterExact("year",year,businessDeptList); businessDeptList = this.filterExact("month",month,businessDeptList); businessDeptList = this.filterVague("deptName",deptName,businessDeptList); businessDeptList = this.filterVague("deptLeader",deptLeader,businessDeptList); if(!StringUtils.isEmpty(jyz)){businessDeptList = this.filterExact("jyz",Double.parseDouble(jyz),businessDeptList); }if(!StringUtils.isEmpty(param)){businessDeptList = this.filterExact("param",Double.parseDouble(param),businessDeptList); }if(!StringUtils.isEmpty(sWorkTime)){businessDeptList = this.filterExact("sWorkTime",Double.parseDouble(sWorkTime),businessDeptList); }if(!StringUtils.isEmpty(rev)){businessDeptList = this.filterExact("rev",Double.parseDouble(rev),businessDeptList); }if(!StringUtils.isEmpty(exp)){businessDeptList = this.filterExact("exp",Double.parseDouble(exp),businessDeptList); }if(!StringUtils.isEmpty(tWorkTime)){businessDeptList = this.filterExact("tWorkTime",Double.parseDouble(tWorkTime),businessDeptList); }}
4.精确匹配
//精确匹配privateList filterExact(String name,T t,List businessDepts){if(t.toString().isEmpty()){return businessDepts; }List list = new ArrayList (); if(businessDepts.size()>0){for(int i = 0; i
5.模糊匹配//模糊匹配private ListfilterVague(String name,String s,List businessDepts){if(s.isEmpty()){return businessDepts; }List list = new ArrayList (); if(businessDepts.size()>0){for(int i = 0; i
【Java利用反射对list对象做过滤】到此这篇关于Java利用反射对list对象做过滤的文章就介绍到这了,更多相关list对象过滤内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JavaScript/jQuery实现切换页面效果
- 以用户注册功能模块为例浅谈MVC架构下的JavaWeb开发流程
- JavaEE|多线程(一)线程和进程的区别
- java|亚马逊跨境电商平台怎么入驻()
- 利用亚马逊云科技整个自用免费网盘
- Java8中Optional操作的实际应用
- JAVA偏向锁的原理与实战
- JavaScript中的Map数据结构详解
- java|使用虹软SDK实现离线人脸识别(局域网)
- javascript|Vue.js全家桶仿哔哩哔哩动画 (移动端APP)