【发布时间】:2015-01-21 14:44:44
【问题描述】:
我正在尝试动态构建 String 和 arraylist 类型的动态哈希图。我有一些来自服务器的 json 数据,而不是声明许多 arraylist,我想将它们保存在 hashmap 中,其中 String 作为键,arraylist 作为值。
这就是我现在正在做的事情
ArrayList<classproperty> allStu;
ArrayList<classproperty> allEmp;
HashMap<String, ArrayList<classproperty>> hash;
if (type.equals("Student")) {
prop = new classproperty("Student", info.getJSONObject(i).getJSONObject("student").getJSONArray("class").getJSONObject(s).getJSONObject("type").getString("name"));
allStu.add(prop);
}
if (type.equals("Emp")) {
prop = new esSignalProperty("Emp", info.getJSONObject(m).getJSONObject("emp").getJSONObject(s).getJSONObject("dept").getString("name"));
allemp.add(prop);
}
hash.put("Student", allStu);
hash.put("Emp", allemp);
所以这是一种丑陋的做法......我想通过直接放入hashmap而不声明这么多arraylist来做到这一点。请忽略 json 字符串提取,因为它只是虚拟的。
【问题讨论】:
-
用空列表预先填充您的 HashMap,对您不起作用?
-
你只用了两个ArrayList,不是很多吗?而且我认为这不是整个代码,对吧? B/c 必须有迭代让所有学生进入 allStu 等。
-
是的,这不是完整的,只是其中的一部分……这只是为了解释想法……
标签: java json arraylist hashmap