private <T> Map<String, BigDecimal> attributeCalculate(List<T> list, Class<T> tClass) {
Field[] declaredFields = tClass.getDeclaredFields();
Map<Field, BigDecimal> map = new HashMap<>();
for (Field field : declaredFields) {
if (field.getType().equals(BigDecimal.class)) {
field.setAccessible(true);
map.put(field, BigDecimal.ZERO);
}
}
Set<Map.Entry<Field, BigDecimal>> entries = map.entrySet();
for (T t : list) {
for (Map.Entry<Field, BigDecimal> entry : entries) {
try {
entry.setValue(entry.getValue().add((BigDecimal) entry.getKey().get(t)));
} catch (IllegalAccessException e) {
throw new BusinessException(OtherException.获取属性值错误);
}
}
}
Map<String, BigDecimal> resultMap = new HashMap<>();
map.forEach((key, value) -> resultMap.put(key.getName(), value));
return resultMap;
}
计算List中对象各个属性值的和
最新推荐文章于 2022-09-21 09:28:54 发布