La méthode max (Comparator <? Super liste <entier >>) dans le type flux <Liste <Entier >> est pas applicable pour les arguments (Comparator <Entier>)

Murali Krishna Konduru:

Je suis en train de chercher le nom de l'étudiant en fonction de ses marques maximum en utilisant l'API de flux de java8.

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

}

Je voudrais revenir le « Rohit » en sortie car il marque max 100 points. s'il est possible de réaliser en une seule opération de flux, comparez toutes les marques et le retour

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

Merci à l'avance et d'apprécier votre aide.

Joakim Danielson:

Pour votre tentative actuelle vous devez d'abord obtenir la valeur maximale pour chaque élève avant de comparer les élèves valeurs max à l'autre

studentData.stream().map(student -> Collections.max(student.getMarks()))
        .max(Comparator.comparing(Integer::intValue)).get();

Le problème avec le code ci-dessus est qu'il retourne la plus haute marque et non l'étudiant donc il doit être réécrite

studentData.stream().sorted((s1, s2) -> Collections.max(s1.getMarks()).compareTo(Collections.max(s2.getMarks())))
    .findFirst().get();

Le code ci-dessus pourrait être plus facile à lire si StudentData peut nous donner la meilleure note

public Integer highestMark() {
    return Collections.max(this.getMarks());
}


studentData.stream().sorted((s1, s2) -> s1.highestMark().compareTo(s2.highestMark())).findFirst().get();

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=199088&siteId=1
conseillé
Classement