gpt4 book ai didi

mapreduce - 使用 map reduce 技术获取 Java8 中多个类字段的摘要

转载 作者:行者123 更新时间:2023-12-01 13:44:54 24 4
gpt4 key购买 nike

下面是学生类定义:

class Student{
String name;
int sub1;
int sub2;
int sub3;

// etc...etc...
}

我有所有学生的名单。要求是获得 sub1、sub2 和 sub3 的平均值。并且还可以为任何学生在任何科目中获得最低分和最高分。

下面是sub1的代码:

    IntSummaryStatistics sub1Summary = students.stream().collect(Collectors.summarizingInt(s -> s.sub1));

是否可以使用单个表达式,或者我必须为每个主题创建 IntSummaryStatistics 并比较每个最低和最高的主题值?

提前致谢

最佳答案

尝试以下操作:

  List<Student> students = Arrays.asList(Student.create("A", 60, 55, 35), 
Student.create("B", 40, 65, 75),
Student.create("C", 70, 45, 95));

Map<String, IntSummaryStatistics> subjectsSummary = new HashMap<String, IntSummaryStatistics>();
Map<String, List<Integer>> subjectMarks = new HashMap<String, List<Integer>>();

List<Integer> sub1Marks = new ArrayList<>();
List<Integer> sub2Marks = new ArrayList<>();
List<Integer> sub3Marks = new ArrayList<>();

// Input : Student
// Output : Map(Key=SubjectName, Value= List of Marks for students)
Function<Student, Map<String, List<Integer>>> toSubjectsMap = (s) -> {
sub1Marks.add(s.getSub1());
sub2Marks.add(s.getSub2());
sub3Marks.add(s.getSub3());

subjectMarks.put("Sub1", sub1Marks);
subjectMarks.put("Sub2", sub2Marks);
subjectMarks.put("Sub3", sub3Marks);

return subjectMarks;
};

//Function to generate summary stats from the list of integers/marks
Function<List<Integer>, IntSummaryStatistics> generateStatsFunc = (listOfMarks) -> {
return listOfMarks.stream().collect(Collectors.summarizingInt(marks->marks));
};


// 1. Students are mapped to generate Map for subject-->List of Marks
// 2. Doing flatMap on the generate Map to get the Keys from the map(list of subjects)
// 3. Collecting the Keys(Subject Names) of Map to Map(Key=SubjectName,Value=IntSummaryStats for Subject)
students.stream().map(toSubjectsMap).flatMap(subMap -> subMap.keySet().stream())
.collect(() -> subjectsSummary/* Initial Value of Map */, (summary, key) -> {
// Generate the stats from the list of marks for the subject
IntSummaryStatistics st = generateStatsFunc.apply(subjectMarks.get(key));
//Adding the stats for each Subject into the result Summary
summary.put(key, st);
} , (summary1, summary2) -> {
//merging the two maps
summary1.putAll(summary2);
});

System.out.println(subjectsSummary);

和学生类如下:

class Student {
String name;
int sub1;
int sub2;
int sub3;

static Student create(String name, int sub1, int sub2, int sub3) {
// validation
return new Student(name, sub1, sub2, sub3);
}

public String getName() {
return name;
}

public int getSub1() {
return sub1;
}

public int getSub2() {
return sub2;
}

public int getSub3() {
return sub3;
}

private Student(String name, int sub1, int sub2, int sub3) {
this.name = name;
this.sub1 = sub1;
this.sub2 = sub2;
this.sub3 = sub3;
}

@Override
public String toString() {
return name;
}
}

关于mapreduce - 使用 map reduce 技术获取 Java8 中多个类字段的摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36691912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com