前言
在项目开发过程中,经常需要筛选List对象数据,不同List对象数据都需要不同的筛选条件与方法,若每个list对象都写一个筛选规则,则会显得代码冗余。对此我们可以利用反射设计不同list对象都可以调用筛选的方法,同时实现任意多条件筛选查询。
一、代码实现
代码传参有泛型实体类和泛型数据列表,主要利用反射获取传入实体对象的get方法获取筛选条件,因此筛选的对象中的字段必须要有get方法。
package com.ld.util;
import com.ld.entity.PageEntity;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Description: 分页工具之列表过滤测试
* @Author: ld
* @Date: 2021-02-02 15:29
*/
public class PageUtil {
private PageUtil() {
}
/**
* @param entity 过滤条件实体
* @param list 待过滤数据列表
* @return 返回过滤后的数据列表
*/
public static <E> List<E> getByEntityFilter(E entity, List<E> list) {
//java8流处理过滤
return list.stream().filter(x -> {
try {
//1.获取传入条件实体对象的所有方法
Method[] methods = entity.getClass().getDeclaredMethods();
List<Boolean> booleanList = new ArrayList<>();
for (Method method : methods) {
//2.筛选出get方法,并且get的数据不为空,作为条件输入。
if (method.getName().contains("get")
&& method.invoke(entity) != null
&& !method.invoke(entity).equals("")) {
//3.将反射获取到的条件值与当前遍历的对象对比,比对上就加入列表保存
booleanList.add(method.invoke(x).toString().equals(method.invoke(entity).toString()));
}
}
//4.若列表中有数据则返回true 标识该条数据符合筛选条件
return booleanList.isEmpty() || booleanList.stream().allMatch(y -> y);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}).collect(Collectors.toList());
}
二、效果测试
1.创建一个测试对象类User
package com.ld.example;
/**
* @Description: 测试对象
* @Author: ld
* @Date: 2021-02-02 17:10
*/
public class User {
private Integer id;
private String name;
private String age;
private String school;
private String home;
private String telephone;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", school='" + school + '\'' +
", home='" + home + '\'' +
", telephone='" + telephone + '\'' +
'}';
}
}
2.创建单元测试
package com.ld;
import com.ld.entity.PageEntity;
import com.ld.example.User;
import com.ld.util.PageUtil;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
/**
* @Description:
* @Author: ld
* @Date: 2021-02-02 17:14
*/
public class test {
String[] strings = new String[]{"a","av","cc","av","ad","xd","va","bc","de","dx"};
List<User> init(){
List<User> list = new ArrayList<>();
for (int i=0;i<strings.length;i++){
int max=100,min=1;
int random = (int) (Math.random()*(max-min)+min);
User user = new User();
user.setId(i);
user.setName(strings[i]);
user.setAge("age_"+strings[i]);
user.setHome("home_"+strings[i]);
user.setSchool("school_"+strings[i]);
user.setTelephone(random+"");
list.add(user);
}
System.out.println("-------------------------初始化数据如下----------------------------");
list.forEach(System.out::println);
return list;
}
/**
* 测试根据实体类的条件筛选数据
*/
@Test
public void testEntityFilter(){
List<User> inList = init();
User user = new User();
//设置筛选条件,可设置多个条件
user.setAge("age_av");
//user.setHome("home_xd");
//user.setId("2")
//根据条件从list中查询回具体数据
List<User> users = PageUtil.getByEntityFilter(user, inList);
System.out.println("------------------------筛选结果如下-------------------------");
users.forEach(System.out::println);
}