繁体   English   中英

Stream <List <Integer >>类型中的max(Comparator <?super List <Integer >>)方法不适用于参数(Comparator <Integer>)

[英]The method max(Comparator<? super List<Integer>>) in the type Stream<List<Integer>> is not applicable for the arguments (Comparator<Integer>)

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

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

您可能正在寻找的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作为以下内容使用void调用:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM