【发布时间】:2021-03-31 15:27:18
【问题描述】:
我的同学如下
class Student{
Map<String,Integer> subjectMarks;
String name;
public Student(Map<String,Integer> subject, String name) {
super();
this.subjectMarks = subject;
this.name = name;
}
public Map<String,Integer> getSubjectMarks() {
return subjectMarks;
}
public void setSubjectMarks(Map<String,Integer> subject) {
this.subjectMarks = subject;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在main方法中,我们在arraylist中添加学生对象如下。
ArrayList<Student> arr = new ArrayList<Student>();
Map m1 = new HashedMap();
m1.put("Maths",40);
m1.put("Science",50);
Map m2 = new HashedMap();
m2.put("Maths",60);
m2.put("Science",20);
arr.add(new Student(m1, "RAJ"));
arr.add(new Student(m2, "AMIT"));
可以帮助/指导我找到每个学生的平均科目分数,然后从平均分数中获得最高分。我需要帮助在 java8 中编写这个 sn-p
【问题讨论】:
-
最好在输出中包含您要查找的内容。使用您的示例,您想要 RAJ 45 分和 AMIT 40 分,还是您想要数学 50 分和科学 35 分?将您的尝试包括到远方和卡住的地方也很有帮助,尤其是因为这看起来像是一项家庭作业。
-
预期输出为 RAJ 45 和 Amit 40,然后从两者中获得最大平均分数,这将是 45。
-
我试图得到输出我知道下面的代码会给你按主题名称的平均值
Map<String, Double> average= arr.stream().map(s -> s.getSubjectMarks()).flatMap(m -> m.entrySet().stream()).collect(Collectors.groupingBy(Entry::getKey, Collectors.averagingDouble(Entry::getValue)));我想得到每个学生姓名的平均分数
标签: java java-8 code-snippets