int total = list.stream().mapToInt(User::getAge).sum();
stream 计算一个List对象中某个字段总和
最新推荐文章于 2024-09-04 03:26:40 发布
博客展示了使用Java Stream计算用户年龄总和的代码,通过list.stream().mapToInt(User::getAge).sum()实现。
博客展示了使用Java Stream计算用户年龄总和的代码,通过list.stream().mapToInt(User::getAge).sum()实现。
int total = list.stream().mapToInt(User::getAge).sum();
1992
354
938