【記錄】JAVA8 Stream()遍歷求某列總和


 

List<CommonExportDTO> commonList;
// 取字段string轉int構建新列表
List<Integer> collect = commonList.stream().map(dto -> Integer.valueOf(dto.getCell6())).collect(Collectors.toList());
// reduce求和 Optional
<Integer> reduce = collect.stream().reduce(Integer::sum); Integer total = reduce.get();

 

參考地址

JDK 1.8 新特性之Stream 詳解個人筆記:https://blog.csdn.net/chenhao_c_h/article/details/80691284

實現一個LIST集合中的某個元素的求和: https://www.cnblogs.com/jishumonkey/p/12166541.html

Java8對List<Integer>的求和:https://blog.csdn.net/hsz2568952354/article/details/89886743


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM