Imagine you have a student gradebook. Each student has marks in each subject in the journal. I want to save this in HashMap<>, but I cannot understand why the tags are combined.
In the journal class :
public class Journal {
private static HashMap<String, HashMap<String, ArrayList<Integer>>> journal = new HashMap<>();
private HashMap<String, ArrayList<Integer>> journalContainer = new HashMap<>();
private ArrayList<Integer> marks = new ArrayList<>();
public void addMark(String student, String subject, int mark) {
marks.add(mark);
journalContainer.put(subject, mark);
journal.put(student, journalContainer);
}
public static void outputMarks() {
for(HashMap.Entry<String, HashMap<String, ArrayList<Integer>>> entry : journal.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
}
}
In the main class :
public class Main {
public static void main(String[] argc) {
getJournal().addMark("Alex", "math", 4);
getJournal().addMark("Alex", "math", 2);
getJournal().addMark("George", "english", 2);
getJournal().addMark("George", "english", 2);
Journal.outputMarks();
}
}
So the output is:
Alex/{english=[4, 2, 2, 2], math=[4, 2, 2, 2]}
George/{english=[4, 2, 2, 2], math=[4, 2, 2, 2]}
But the correct conclusion should be:
Alex/{math=[4, 2]}
George/{english=[2, 2]}
I cannot understand what I am doing wrong. Who can help?