下面是数据库查到的一个 list (合计列为空),我想要得到每一行的合计值,也就是相当于增加一列合计。
public ListPlanDurian(String date) { List list = loutsFruitPadMapper.PlanDurian(date); // 通过反射,计算每一行的合计值 list.forEach(store -> { // 获取所有属性 Field[] fields = store.getClass().getDeclaredFields(); int total = 0; for (Field field : fields){ // 允许拿实体类里的所有private属性 field.setAccessible(true); // if ("total".equals(field.getName()) || "code".equals(field.getName())){ continue; } int num; try { num = field.get(store) == null ? 0 : Integer.parseInt((String)field.get(store)); }catch (Exception e){ num = 0; } total = total + num; } store.setTotal(total); }); list = list.stream().sorted((o1,o2) -> o2.getTotal().compareTo(o1.getTotal())).collect(toList()); return list; }
结果:



