First, you have an error when adding to Map . You always use info . As a result, you reuse it, instead of actually creating three different StudentInfo . As a result, in info you will always have only the last data you added. and it turns out that all three students have values:
6.0 8.6 10.0
To avoid this, you need to create separate objects for each student. For example, if the data is set at initialization (although this cannot be said for the setMinimal / setAverage / setMaximal methods), then it is enough to declare a constructor to which to transfer initialization parameters:
public StudentInfo(float min,float avg, float max){ minimal = min; average = avg; maximal = max; }
And then the data will be filled in like this:
Map<String, StudentInfo> studentsInfo = new HashMap<String, StudentInfo>(); studentsInfo.put("Вася Пупкін", new StudentInfo(7f, 8.4f, 10f)); studentsInfo.put("Нехлюй Петро", new StudentInfo(9f, 10.1f, 11f)); studentsInfo.put("Придорожний Семен", new StudentInfo(6f, 8.6f, 10f));
To display the maximum score of each student, you simply need to loop through the Map and output the relevant data using get methods.
System.out.println("Максимальный бал студентов: "); for (Map.Entry<String, StudentInfo> entry : studentsInfo.entrySet()) { StudentInfo studentInfo = entry.getValue(); System.out.println(entry.getKey() + ": " + studentInfo.getMaximal()); }
If it was meant to withdraw students with a maximum score at all, then for this you need
- Find what all students score the maximum
- Select on this basis students.
Java8 streaming example:
public static Map<String, StudentInfo> getStudentWithMax(Map<String, StudentInfo> students){ float maxItem = students.entrySet().stream().map(el -> el.getValue().getMaximal()).max(Float::compareTo).get(); Map<String, StudentInfo> result = students.entrySet().stream().filter(num -> num.getValue().getMaximal() == maxItem).collect(Collectors.toMap( e -> e.getKey(), e -> e.getValue() )); return result; } public static void main(String[] args) throws Exception { Map<String, StudentInfo> students = new HashMap<String, StudentInfo>(); students.put("Вася Пупкін", new StudentInfo(7f, 8.4f, 10f)); students.put("Нехлюй Петро", new StudentInfo(9f, 10.1f, 11f)); students.put("Придорожний Семен", new StudentInfo(6f, 8.6f, 11f)); System.out.println("Студенты с максимальным баллом: "); Map<String, StudentInfo> studentWithMax = getStudentWithMax(students); for (Map.Entry<String, StudentInfo> stringStudentInfoEntry : studentWithMax.entrySet()) { System.out.println(stringStudentInfoEntry.getKey()); } }
An example without Java8 streams
public static Map<String, StudentInfo> getStudentWithMax2(Map<String, StudentInfo> students){ float max = 0f; Map<String, StudentInfo> result = new HashMap<>(); for (Map.Entry<String, StudentInfo> entry : students.entrySet()) { StudentInfo studentInfo = entry.getValue(); if (studentInfo.getMaximal() == max) result.put(entry.getKey(), studentInfo); if (studentInfo.getMaximal() > max) { max = studentInfo.getMaximal(); result.clear(); result.put(entry.getKey(), studentInfo); } } return result; } public static void main(String[] args) throws Exception { Map<String, StudentInfo> students = new HashMap<String, StudentInfo>(); students.put("Вася Пупкін", new StudentInfo(7f, 8.4f, 10f)); students.put("Нехлюй Петро", new StudentInfo(9f, 10.1f, 11f)); students.put("Придорожний Семен", new StudentInfo(6f, 8.6f, 11f)); System.out.println("Студенты с максимальным баллом: "); Map<String, StudentInfo> studentWithMax = getStudentWithMax2(students); for (Map.Entry<String, StudentInfo> stringStudentInfoEntry : studentWithMax.entrySet()) { System.out.println(stringStudentInfoEntry.getKey()); } }