Java 类型流中的方法max(Comparator<;?super List<;Integer>;);列表<;整数>&燃气轮机;不适用于参数(比较器<;整数>;)
我正在尝试使用java8 streams API根据学生的最高分数获取学生姓名Java 类型流中的方法max(Comparator<;?super List<;Integer>;);列表<;整数>&燃气轮机;不适用于参数(比较器<;整数>;),java,java-8,java-stream,Java,Java 8,Java Stream,我正在尝试使用java8 streams API根据学生的最高分数获取学生姓名 import static java.util.Arrays.asList; public class Main { public static void main(String args[]) { new StudentReport(asList( new StudentData("Rohit", asList(100, 81, 82, 83)),
import static java.util.Arrays.asList;
public class Main {
public static void main(String args[]) {
new StudentReport(asList(
new StudentData("Rohit", asList(100, 81, 82, 83)),
new StudentData("Ram", asList(84, 85, 86, 87))));
}
}
import java.util.List;
public class StudentData {
private String name;
private List<Integer> marks;
StudentData(String name, List<Integer> marks) {
this.name = name;
this.marks = marks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Integer> getMarks() {
return marks;
}
public void setMarks(List<Integer> marks) {
this.marks = marks;
}
}
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
public class StudentReport {
public StudentReport(List<StudentData> studentData)
{
printStudentNameWithMaxMarks(studentData);
}
private void printStudentNameWithMaxMarks(List<StudentData> studentData) {
// I am getting erro here.
studentData.stream().map(student -> student.getMarks())
.max(Comparator.comparing(Integer::intValue)).get();
}
}
提前感谢并感谢您的帮助。您可能要查找的
学生数据可以通过以下方式获得:
Optional<StudentData> studentWithMaxMarksInAnySubject = studentData
.stream()
// student with highest marks in any subject when there individual max scores are compared
.max(Comparator.comparingInt(a ->
a.getMarks().stream()
.mapToInt(i -> i)
.max() // max marks per student
.orElse(0)));
对于您当前的尝试,您需要先获取每个学生的最大值,然后再将学生的最大值相互比较
studentData.stream().map(student -> Collections.max(student.getMarks()))
.max(Comparator.comparing(Integer::intValue)).get();
上述代码的问题在于它返回的是最高分数,而不是学生,因此需要重写
studentData.stream().sorted((s1, s2) -> Collections.max(s1.getMarks()).compareTo(Collections.max(s2.getMarks())))
.findFirst().get();
如果StudentData能给我们最高分数,那么上面的代码就更容易阅读了
public Integer highestMark() {
return Collections.max(this.getMarks());
}
studentData.stream().sorted((s1, s2) -> s1.highestMark().compareTo(s2.highestMark())).findFirst().get();
studentData.stream().map(student->student.getMarks())
是一个流。因此,如果你想得到它的max元素,这是一个列表,你需要一个比较器,而不是比较器。如果你的目标是打印得分最高的学生的名字,那么首先在StudentData中添加一个getHighestMark()方法,它返回一个学生的最高分数。然后使用比较器将最高分数的学生与最高分数的学生进行比较,得到最高分数的学生。如果两个学生的最高分数相同,那么应该返回什么?但其他分数则不同。是否也应该检查其他分数?@HadiJ如果它可以同时返回两个名称,那么对于相同的最大分数就更足够了。我一直在努力寻找这种逻辑,非常感谢您的帮助,谢谢。您可以(并且应该)使用min(…)
,而不是sorted(…)
。嗯,OP想要max(…)
。
studentData.stream().sorted((s1, s2) -> Collections.max(s1.getMarks()).compareTo(Collections.max(s2.getMarks())))
.findFirst().get();
public Integer highestMark() {
return Collections.max(this.getMarks());
}
studentData.stream().sorted((s1, s2) -> s1.highestMark().compareTo(s2.highestMark())).findFirst().get();