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) {
}
public void setMonth(String 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) {
}
public void setExp(Double 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 = Class();
Field[] fields = cl.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
if (Name().equals(name)) {
return (E) (this);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
2.根据字段名获取value public <E> E get(String name) {
Class cl = Class();
Field[] fields = cl.getDeclaredFields();
try {
for (Field field : fields) {
field.setAccessible(true);
if (Name().equals(name)) {
return (E) (this);
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
3.对list过滤
List businessDeptList = Query();
//筛选符合条件的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); }
}
4.精确匹配
字符串转数组去除空格//精确匹配
private <T>List<BusinessDept> filterExact(String name,T t,List<BusinessDept> businessDepts){
String().isEmpty()){
return businessDepts;
}
List<BusinessDept> list = new ArrayList<BusinessDept>();
if(businessDepts.size()>0){
for(int i = 0;i<businessDepts.size();i++){
(i).get(name).equals(t)){
list.(i));
}
}
}
return list;
}
5.模糊匹配
/
/模糊匹配
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++){
(i).get(name).toString().contains(s)){
list.(i));
}
}
}
return list;
}
到此这篇关于Java利⽤反射对list对象做过滤的⽂章就介绍到这了,更多相关list对象过滤内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。