您好,登錄后才能下訂單哦!
小編給大家分享一下Java如何利用反射對list對象做過濾,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
反射對效率有影響 慎用!!!
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; } public <E> E 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; } }
public <E> E 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; }
List businessDeptList = this.myQuery(); //篩選符合條件的list if (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); } }
//精確匹配 private <T>List<BusinessDept> filterExact(String name,T t,List<BusinessDept> businessDepts){ if(t.toString().isEmpty()){ return businessDepts; } List<BusinessDept> list = new ArrayList<BusinessDept>(); if(businessDepts.size()>0){ for(int i = 0;i<businessDepts.size();i++){ if(businessDepts.get(i).get(name).equals(t)){ list.add(businessDepts.get(i)); } } } return list; }
//模糊匹配 private List<BusinessDept> filterVague(String name,String s,List<BusinessDept> businessDepts){ if(s.isEmpty()){ return businessDepts; } List<BusinessDept> list = new ArrayList<BusinessDept>(); if(businessDepts.size()>0){ for(int i = 0;i<businessDepts.size();i++){ if(businessDepts.get(i).get(name).toString().contains(s)){ list.add(businessDepts.get(i)); } } } return list; }
看完了這篇文章,相信你對“Java如何利用反射對list對象做過濾”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。