根据List中的对象的某个属性,求和

一、根据List中的对象的某个属性,求和

double mathAverageInt = list.stream().mapToInt( Student::getMathScoresInt ).sum().orElse(0d);
double mathAverageLong = list.stream().mapToLong( Student::getMathScoresLong ).sum().orElse(0d);
double mathAverageDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).sum().orElse(0d);
BigDecimal kczl = entities.stream().map(BloodRkdEntity::getKcxl).reduce(BigDecimal.ZERO, BigDecimal::add);

二、根据List中的对象的某个属性,求平均值

double mathAverageInt = list.stream().mapToInt( Student::getMathScoresInt ).average().orElse(0d);
double mathAverageLong = list.stream().mapToLong( Student::getMathScoresLong ).average().orElse(0d);
double mathAverageDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).average().orElse(0d);

三、根据List中的对象的某个属性,求最大值

int mathMaxInt = list.stream().mapToInt( Student::getMathScoresInt ).max().getAsInt(); //int类型
long mathMaxLong = list.stream().mapToLong( Student::getMathScoresLong ).max().getAsLong();
double mathMaxDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).max().getAsDouble();
BigDecimal mathMaxBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::max);

四、根据List中的对象的某个属性,求最小值

int mathMinInt = list.stream().mapToInt( Student::getMathScoresInt ).min().getAsInt();
long mathMinLong = list.stream().mapToLong( Student::getMathScoresLong ).min().getAsLong();
double mathMinDouble = list.stream().mapToDouble( Student::getMathScoresDouble ).min().getAsDouble();
BigDecimal mathMinBigDecimal = list.stream().map( Student::getMathScoresBigDecimal ).reduce(BigDecimal.ZERO, BigDecimal::min);
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: Java 可以使用 Stream API 和 Lambda 表达式来对 List 的某个字段求和。 代码示例: ``` List<Person> people = ... int sum = people.stream() .mapToInt(p -> p.getAge()) .sum(); ``` 其,Person 是实体类,getAge() 方法返回该实体类 age 字段的值。 也可以使用 reduce 方法进行求和,下面是例子 ``` int sum = people.stream().map(Person::getAge).reduce(0, Integer::sum); ``` 通过这种方式,你可以很方便地对 List 的某个字段求和。 ### 回答2: 在Java,我们可以通过使用List集合和循环遍历的方式来实现对某个字段的求和操作。 首先,我们需要定义一个List集合来存储对象。假设我们有一个名为"students"的List集合,其存储了多个学生对象,每个学生对象都有一个名为"score"的字段表示分数。 接下来,我们可以创建一个变量来保存求和结果,比如命名为"sum",并初始化为0。然后,我们可以使用for循环遍历整个List集合,对每个学生对象的分数进行累加操作。 具体的代码如下所示: ```java List<Student> students = new ArrayList<>(); // 学生列表 int sum = 0; // 分数的总和 for (Student student : students) { sum += student.getScore(); // 对每个学生的分数进行累加 } System.out.println("分数的总和为:" + sum); // 打印结果 ``` 需要注意的是,上述代码的"Student"类是一个自定义类,其包含了一个名为"score"的成员变量,以及相应的getter方法"getScore()"来获取分数值。在实际使用时,请替换为你所使用的对象和字段。 通过上述代码,我们就可以以很简洁的方式对List集合的某个字段进行求和操作。 ### 回答3: 在Java,可以使用List来存储一系列的对象。要通过某个字段对List的元素进行求和,可以遵循以下步骤: 1. 创建一个变量用于存储求和的结果,初始化为0。 2. 遍历List的每个元素。 3. 对于每个元素,访问它的某个字段的值,可以使用对象的getter方法或直接访问字段。 4. 将每个元素的字段值加到求和的结果。 5. 遍历完所有元素后,求和的结果就是所需的值。 以下是用Java代码实现以上步骤的示例: ```java import java.util.List; public class Main { private static class Item { private int value; public Item(int value) { this.value = value; } public int getValue() { return value; } } public static void main(String[] args) { List<Item> items = List.of(new Item(1), new Item(2), new Item(3), new Item(4)); int sum = 0; for (Item item : items) { sum += item.getValue(); } System.out.println("求和结果: " + sum); } } ``` 在上述示例,我们创建了一个Item类来表示列表的每个元素,每个元素都有一个value字段。然后,我们使用List.of方法创建了一个包含4个Item对象List。我们通过遍历List的每个Item对象,并使用getValue方法获取每个Item的值,并将其累加到sum变量。最终,我们打印sum的值,即为通过某个字段求和的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值