java list取前幾條,【java】計算 list 中每一行的合計值

 2023-10-21 阅读 24 评论 0

摘要:下面是數據庫查到的一個 list (合計列為空),我想要得到每一行的合計值,也就是相當于增加一列合計。 public List<FruitPlanEntity> PlanDurian(String date) {List<FruitPlanEntity> list = loutsFruitPadMapper.PlanDurian(date

下面是數據庫查到的一個 list (合計列為空),我想要得到每一行的合計值,也就是相當于增加一列合計。
在這里插入圖片描述

public List<FruitPlanEntity> PlanDurian(String date) {List<FruitPlanEntity> 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;
}

java list取前幾條、結果:
在這里插入圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/153878.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息