Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java uses reflection to filter list objects

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report