国产ChatGPT来了
上链接
来个例子
Java List<EvaluationItemRecordDTO> 按照evaluationItemId分组 按照score求平均值最后求和
import java.util.*;import java.util.stream.Collectors;public class Main { public static void main(String[] args) { // 假设有以下评估记录 List<EvaluationItemRecordDTO> records = new ArrayList<>(); records.add(new EvaluationItemRecordDTO(1, 80)); records.add(new EvaluationItemRecordDTO(2, 90)); records.add(new EvaluationItemRecordDTO(1, 70)); records.add(new EvaluationItemRecordDTO(1, 85)); records.add(new EvaluationItemRecordDTO(2, 95)); // 按照evaluationItemId分组,按照score求平均值 Map<Integer, Double> avgScoreByItemId = records.stream() .collect(Collectors.groupingBy( EvaluationItemRecordDTO::getEvaluationItemId, Collectors.averagingDouble(EvaluationItemRecordDTO::getScore))); // 输出每个评估项的平均分数 System.out.println("每个评估项的平均分数:"); for (Map.Entry<Integer, Double> entry : avgScoreByItemId.entrySet()) { System.out.println("评估项" entry.getKey() "的平均分数为:" entry.getValue()); } // 求所有评估项平均分数的总和 double sumOfAvgScores = avgScoreByItemId.values().stream() .mapToDouble(Double::doubleValue) .sum(); System.out.println("所有评估项平均分数的总和为:" sumOfAvgScores); }}class EvaluationItemRecordDTO { private int evaluationItemId; private int score; public EvaluationItemRecordDTO(int evaluationItemId, int score) { this.evaluationItemId = evaluationItemId; this.score = score; } public int getEvaluationItemId() { return evaluationItemId; } public void setEvaluationItemId(int evaluationItemId) { this.evaluationItemId = evaluationItemId; } public int getScore() { return score; } public void setScore(int score) { this.score = score; }}