Java设计模式之外观模式示例详解
340
2022-08-17
Java利用反射对list对象做过滤
目录1.对象结构2.根据字段名获取value3.对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;
}
public
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
public
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 = thiWwOCDs.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.parseDoubleWwOCD(tWorkTime),businessDeptList);
}
}
4.精确匹配
//精确匹配
private
if(t.toString().isEmpty()){
return businessDepts;
}
List
if(businessDepts.size()>0){
for(int i = 0;i if(businessDepts.get(i).get(name).equals(t)){ list.add(businessDepts.get(i)); } } } return list; } 5.模糊匹配 //模糊匹配 private List if(s.isEmpty()){ return businessDepts; } List if(businessDepts.size()>0){ for(int i = 0;i if(businessDepts.get(i).get(name).toString().contains(s)){ list.add(businessDepts.get(i)); } } } return list; }
if(businessDepts.get(i).get(name).equals(t)){
list.add(businessDepts.get(i));
}
}
}
return list;
}
5.模糊匹配
//模糊匹配
private List
if(s.isEmpty()){
return businessDepts;
}
List
if(businessDepts.size()>0){
for(int i = 0;i if(businessDepts.get(i).get(name).toString().contains(s)){ list.add(businessDepts.get(i)); } } } return list; }
if(businessDepts.get(i).get(name).toString().contains(s)){
list.add(businessDepts.get(i));
}
}
}
return list;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~