1.例子
原型:
date,h1,h2,h3,h4....h24
变换后:
h1,
h2,
h3,
...
h24
2.转换的代码
public static Map<Integer, BigDecimal> getClazzMapInfo(Object obj) throws Exception {
Map<Integer, BigDecimal> resultMap = new HashMap<>();
// 获取对象属性列表
Field[] fields = obj.getClass().getDeclaredFields();
int i = 1;
for (Field field : fields) {
if(field.getName().startsWith("h")){
// 允许访问私有属性
field.setAccessible(true);
if(field.get(obj)!=null){
resultMap.put(i, new BigDecimal(field.get(obj).toString()));
}
i = i + 1;
}
}
return resultMap;
}
3.求和代码
public static Map<String, BigDecimal> getClazzInfoByDecimal(Object obj) throws Exception {
Map<String, BigDecimal> resultMap = new HashMap<>();
BigDecimal total = new BigDecimal("0");
// 获取对象属性列表
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
if(field.getName().startsWith("h")||field.getName().startsWith("v")){
// 允许访问私有属性
field.setAccessible(true);
if(field.get(obj)!=null){
total = total.add(new BigDecimal(field.get(obj).toString()));
}
}
}
resultMap.put("total", total);
return resultMap;
}