public static <T> void filter(List<T> list, String[] fieldNames, Object[] fieldValues) {
List<T> filteredList = new ArrayList<>();
for (T object : list) {
boolean isMatch = true;
for (int i = 0; i < fieldNames.length; i++) {
try {
Field field = object.getClass().getDeclaredField(fieldNames[i]);
field.setAccessible(true);
Object value = field.get(object);
if (!value.equals(fieldValues[i])) {
isMatch = false;
break;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
if (isMatch) {
filteredList.add(object);
}
}
list.clear();
list.addAll(filteredList);
}
传入对应的字段名和值,取出集合
最新推荐文章于 2024-08-29 18:44:48 发布