I want to iterate though this 2d array and put values into a Map of Strings as key and list of strings Basically i have this but i am not able to modify the value list to add another value in the case where a key already exists
static float bestAverageStudent(String[][] students) {
Map<String, List<String>> grades = new HashMap<>();
for (int row = 0; row < students.length; row++) {
for (int col = 0; col < students[row].length; col++) {
if (grades.get(students[row][0]) == null) {
grades.put(students[row][0], Arrays.asList(students[col][1]));
} else {
List<String> strings1 = grades.get(students[row][0]);
strings1.add( students[col][1]); //It fails when i try to add to the list
grades.put(students[row][0], new ArrayList<>(strings1));
}
}
}
System.out.println(grades);
return 0;
Here is the array
public static String students[][] = new
String[][]{{"jerry", "65"},
{"bob", "91"},
{"jerry", "23"},
{"Eric", "83"}};
I would like to keep this record in a map where values are a list of grades by one student
Thank you