类型为Stream <list <integer >>的方法max(Comparator <!-?super List <Integer->>)不适用于参数(Comparator <integer>)

时间:2019-06-01 05:53:25

标签: java java-8 java-stream

我正在尝试使用java8流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)),
                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();
    }

}

我想在输出中返回“ Rohit”,因为他得分最高为100分。是否有可能在单个流操作中实现,比较所有标记并返回

new StudentData("Rohit", asList(100, 81, 82, 83)),
new StudentData("Ram", asList(84, 85, 86, 87))));

在此先感谢并感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可能正在寻找的StudentData可以使用。:

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)));

要打印此类学生的姓名,您可以使用ifPresent进行无效呼叫,如下所示:

studentData
        .stream()
        .max(Comparator.comparingInt(a ->
                a.getMarks().stream()
                        .mapToInt(i -> i)
                        .max()
                        .orElse(0)))
        .map(StudentData::getName)
        .ifPresent(System.out::println);

答案 1 :(得分:2)

对于当前的尝试,您需要先获取每个学生的最大值,然后再将学生的最大值进行比较

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();