Rumah > Java > javaTutorial > teks badan

Cara menggunakan pantulan untuk menapis objek senarai di Jawa

PHPz
Lepaskan: 2023-05-02 10:43:06
ke hadapan
1405 orang telah melayarinya

Cara menggunakan pantulan untuk menapis objek senarai di Jawa

Refleksi memberi kesan kepada kecekapan Gunakan dengan berhati-hati! ! !

1. Struktur objek

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;
}
}
Salin selepas log masuk

2. Dapatkan nilai mengikut nama medan

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;
}
Salin selepas log masuk

3. Senarai penapis

rreee

4

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);
}

}
Salin selepas log masuk

5. Padanan kabur

//精确匹配
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;
}
Salin selepas log masuk

Atas ialah kandungan terperinci Cara menggunakan pantulan untuk menapis objek senarai di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:yisu.com
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan