Skip to content

Instantly share code, notes, and snippets.

@tremaineeto
Created May 1, 2021 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tremaineeto/89bcfc55714ea3bc5ddb1142889bc43e to your computer and use it in GitHub Desktop.
Save tremaineeto/89bcfc55714ea3bc5ddb1142889bc43e to your computer and use it in GitHub Desktop.
String[][] peopleToGrades = {
{"Tremaine", "A"},
{"Jeremy", "A-"},
{"Yuta", "B"},
{"Steph", "A+"},
{"Klay", "B+"},
{"Damian", "A"},
{"Klay", "A"},
{"Jeremy", "B-"},
};
HashMap<String, ArrayList<String>> peopleToGradesMap = new HashMap<>();
for (int i = 0; i < peopleToGrades.length; i++) {
if (!peopleToGradesMap.containsKey(peopleToGrades[i][0])) { // [i][0] is the name of the person
peopleToGradesMap.put(peopleToGrades[i][0], new ArrayList<String>()); // ArrayList is empty at this point
peopleToGradesMap.get(peopleToGrades[i][0]).add(peopleToGrades[i][1]);
} else {
peopleToGradesMap.get(peopleToGrades[i][0]).add(peopleToGrades[i][1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment