list stream 求和
时间: 2023-09-13 18:07:19 浏览: 96
你可以使用Java 8的Stream来对List进行求和操作。如果List中的元素是对象,并且你需要对特定属性进行求和,可以使用mapToLong方法,将属性映射为Long类型,然后使用sum方法进行求和。例如,如果List中的元素是User对象,你可以按如下方式求和年龄:int sum = list.stream().mapToLong(User::getAge).sum();<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Java8 list利用stream求和](https://blog.csdn.net/jiafujun_fx/article/details/113524632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Java stream List 求和、分组操作](https://blog.csdn.net/weixin_29576901/article/details/114927443)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文