The problem is that I don’t know how to display any object of type ua.edu.kep.LR_7.StudentInfo. For example, how do you get the maximum score of all students on the screen studentsInfo on the screen?

Class StudentsMap:

package ua.edu.kep.LR_7; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; public class StudentsMap { public static void main(String[] args) { Map<String, StudentInfo> studentsInfo = new HashMap<String, StudentInfo>(); StudentInfo info = new StudentInfo(); info.setMinimal((float) 7); info.setAverage((float) 8.4); info.setMaximal((float) 10); studentsInfo.put("Вася Пупкін", info); info.setMinimal((float) 9); info.setAverage((float) 10.1); info.setMaximal((float) 11); studentsInfo.put("Нехлюй Петро", info); info.setMinimal((float) 6); info.setAverage((float) 8.6); info.setMaximal((float) 10); studentsInfo.put("Придорожний Семен", info); } } 

StudentInfo class:

 package ua.edu.kep.LR_7; public class StudentInfo { private float minimal; private float average; private float maximal; public float getMinimal(){ return minimal; } public void setMinimal( float value ){ minimal = value; } public float getAverage(){ return average; } public void setAverage( float value ){ average = value; } public float getMaximal(){ return maximal; } public void setMaximal( float value ){ maximal = value; } } 
  • More specifically. What exactly need to withdraw? - Flippy

1 answer 1

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

  • Thank you, Alexey, everything works! The last two examples were not required. You understood everything right from the start. But I ran into another problem. How to display all the balls (max, min, average) of all students? Or is this another question and another topic of conversation? - Vladimir
  • one
    @ Vladimir what does all the points mean? both minimum and average and maximum? Everything is absolutely the same as with the maximum. Only instead of getMaximal() would be for example System.out.println(entry.getKey() + ": " + "max - " + studentInfo.getMaximal() + ", min - "+ studentInfo.getMinimal() + ", avg - " + studentInfo.getAverage()); ......... in what form to output - it's up to you ..... the data is already available via get - just take it - Alexey Shimansky
  • Thanks, I already guessed) It's very simple! - Vladimir
  • Can the last question? If you want to display a list of names of students who have equal minimum points or average points or maximum points, how can this be done? - Vladimir
  • @Vladimir, it is more likely to issue another issue, but it will be in the same style as I wrote a sample of students with the maximum value. That is, through additional selections and filters in the form of students.entrySet().stream().filter(.... , etc. ............ by the way, I see no reason to make Map<String, StudentInfo> . Rather, you need to create a new class Students in the cat his name will be stored as well as his infa according to estimates in the form of fields, or in the form of a variable with the type StudentInfo . IMHO - the best option - Alexey Shimansky