java-在HashMap中包含适当的数据 [英] java - content appropriate data in HashMap

查看:132
本文介绍了java-在HashMap中包含适当的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您有一本学生评价日记。每个学生在日记中对每个主题都有一些标记。我想将其存储在 HashMap<> 中,但我不知道为什么标记会合并。

Imagine you have a journal of student evaluations. Each student have some marks of each subject in a journal. I want to store this in HashMap<> but I can't figure out why do marks combine.

在日记类中

public class Journal {
    private static HashMap<String, HashMap<String, ArrayList<Integer>>> journal = new HashMap<>(); // "Student" -> "Subject", mark[]

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

在主要班级

public class Main {
    public static void main(String[] argc) {
        getJournal().addMark("Alex", "math", 4); // name, subject, mark
        getJournal().addMark("Alex", "math", 2);
        getJournal().addMark("George", "english", 2);
        getJournal().addMark("George", "english", 2);

        Journal.outputMarks();
    }
}

因此输出为:

Alex/{english=[4, 2, 2, 2], math=[4, 2, 2, 2]}
George/{english=[4, 2, 2, 2], math=[4, 2, 2, 2]}

但是正确的输出应该是:

But the right output should be:

Alex/{math=[4, 2]}
George/{english=[2, 2]}

我不知道我是什么做错了。有人可以帮忙吗?

I can't figure out what am I doing wrong. Anyone can help?

推荐答案

问题是您只有一个 marks = new ArrayList<> (); 实例,您将其用作所有内部 Map 中的值,以及单个 journalContainer = new HashMap<>(); 实例,该实例用作外部 Map 中的值。

The problem is that you have a single marks = new ArrayList<>(); instance that you use as values in all your inner Maps, as well as a single journalContainer = new HashMap<>(); instance that you use as values in your outer Map.

我将删除这两个实例变量,而是使用局部变量。

I'd remove these two instance variables, and instead use local variables.

您应该使用不同的 ArrayList s和内部 HashMap s作为 Map s的值:

You should use distinct ArrayLists and inner HashMaps as values of your Maps:

public void addMark(String student, String subject, int mark) {
    HashMap<String, ArrayList<Integer>> journalContainer = journal.get(student);
    if (journalContainer == null) {
        journalContainer = new HashMap<>();
        journal.put(student,journalContainer);
    }
    ArrayList<Integer> marks = journalContainer.get(subject);
    if (marks == null) {
        marks = new ArrayList<>();
        journalContainer.put(subject, marks);
    }
    marks.add(mark);
}

BTW,让实例方法修改a静态成员(新闻 地图)。将方法设为静态,或将 Map 设为非静态。

BTW, it doesn't make sense to have an instance method that modifies a static member (the journal Map). Either make the method static, or make the Map non static.

这篇关于java-在HashMap中包含适当的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆