这是我的:
HashMap<String,HashMap<Integer,Integer>> data =
new HashMap<String,HashMap<Integer,Integer>>();
但是我无法为此添加值,因为内部散列映射没有名称(注意:它不应该有)。我实际上正在尝试将数组列表添加到 HashMap 中的第一个 Integer 所以我正在尝试类似的东西:
data.put(var, data.get(array.get(x), y));
它非常不喜欢,我完全不知道该怎么做。
最佳答案
注意
HashMap<String,HashMap<Integer,Integer>> data =
new HashMap<String,HashMap<Integer,Integer>>();
仅创建“外部”HashMap 实例。在这条语句之后,您有一个空的 HashMap,它将字符串作为键,将 HashMap<Integer, Integer>
作为值。
您可以通过以下方式将 HashMap<Integer, Integer>
实例添加到 data
:
data.put("myKey", new HashMap<Integer, Integer>());
之后,您可以将 Integer 值添加到第二个 HashMap:
data.get("myKey").put(123, 456); // use 123 as key and 456 as value
取回值:
data.get("myKey").get(123); // returns 456
关于java - 添加到 hashmap 内部的 hashmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15601924/