HashMap<String, HashMap<String, String>> hm = new HashMap<String, HashMap<String, String>>();
hm.put("Title1","Key1");
for(int i=0;i<2;i++) {
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("Key1","Value1");
}
如果我当时调用了 Title1,他们调用了另一个 HashMap 。我想 这种类型的输出
hm<key,value(object hm1)>
hm<key,value)
第一个 HashMap 对象调用第二个 HashMap 键
最佳答案
如果我纠正你不明白你想要什么,使用下面的代码
HashMap<String, HashMap<String, String>> hm = new HashMap<>();
HashMap<String, String> hm1 = new HashMap<>();
for(int i=0;i<2;i++) {
hm1.put("Key1","Value1");
}
hm.put("Title1", hm1); // save hm
...
HashMap<String, String> hm2 = hm.get("Title1");
String s = hm2.get("Key1"); // s = "Value1"
或者你可以创建新类
class HashKey {
private String title;
private String key;
...
// getters, setters, constructor, hashcode and equals
}
并且只使用 HashMap < HashKey, String > hm,例如:
hm.put(new HashKey("Title1", "Key 1"), "Value");
...
String s = hm.get(new HashKey("Title1", "Key 1")); // Value
关于java - 如何使用其他 HashMap 的对象在 HashMap 中定义 HashMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34067618/