【发布时间】:2014-11-01 15:13:34
【问题描述】:
我知道这是一个真正的初学者问题,但我别无选择 :-) 我需要在 Velocity 中编写一个程序,该程序将采用 Map 并写入所有值及其键(不是键及其值!!!) - 每个值都允许记录在更多键下。
如果你能做到,请帮助我,我会感激每一个答案:-) 非常感谢!
在java中是这样的:
HashMap<String, String> map = new HashMap<String, String>();
map.put("key1", "val1");
map.put("key2", "val1");
map.put("key3", "val2");
map.put("key4", "val1");
HashMap<String, ArrayList<String>> hmap = new HashMap<String, ArrayList<String>>();
for (Entry<String, String> item : map.entrySet()) {
String val = item.getValue();
if (!hmap.containsKey(val)) {
hmap.put(val, new ArrayList<String>());
hmap.get(val).add(item.getKey());
} else {
hmap.get(val).add(item.getKey());
}
}
for (Entry<String, ArrayList<String>> item : hmap.entrySet()) {
System.out.println(item.getKey() + " " + item.getValue());
}
【问题讨论】:
标签: java map hashmap key velocity