How Java uses reflection to filter list objects
Editor to share with you how Java uses reflection to filter list objects. I hope you will get something after reading this article. Let's discuss it together.
Reflection has an impact on efficiency. Use it carefully!
1. Object structure public class BusinessDept {private String year;// private String month;// monthly private String deptName;// department name private String deptLeader;// department head private Double jyz; / / operating value private Double rev; / / income private Double exp; / / expenditure private Double tWorkTime; / / fill in man-hour private Double sWorkTime; / / standard man-hour private Double param; / / operating parameter 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 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. Get valuepublic 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.printStackTrace ();} return null;} 3. Filter List businessDeptList = this.myQuery () for list; / / filter eligible 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. Exact match / / exact match private List filterExact (String name,T tMagneList businessDepts) {if (t.toString (). IsEmpty ()) {return businessDepts;} List list = new ArrayList (); if (businessDepts.size () > 0) {for (int I = 0 position i0) {for (int I = 0 leading I)