Java Velocity打印值及其键
我知道这是一个真正的初学者问题,但我别无选择:-)我需要用Velocity编写一个程序,它将获取一个映射并写入所有值及其键(而不是键及其值!!!)-允许在更多键下记录每个值 如果您能做到这一点,请帮助我,我将感谢您的每一个回答:-) 多谢各位 在java中,它如下所示:Java Velocity打印值及其键,java,map,hashmap,key,velocity,Java,Map,Hashmap,Key,Velocity,我知道这是一个真正的初学者问题,但我别无选择:-)我需要用Velocity编写一个程序,它将获取一个映射并写入所有值及其键(而不是键及其值!!!)-允许在更多键下记录每个值 如果您能做到这一点,请帮助我,我将感谢您的每一个回答:-) 多谢各位 在java中,它如下所示: HashMap<String, String> map = new HashMap<String, String>(); map.put("key1", "val1"); map.
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());
}
HashMap map=newhashmap();
地图放置(“键1”、“值1”);
地图放置(“键2”、“值1”);
地图放置(“键3”、“值2”);
地图放置(“键4”、“值1”);
HashMap hmap=新的HashMap();
对于(条目项:map.entrySet()){
字符串val=item.getValue();
如果(!hmap.containsKey(val)){
hmap.put(val,newarraylist());
获取(val).add(item.getKey());
}否则{
获取(val).add(item.getKey());
}
}
对于(条目项:hmap.entrySet()){
System.out.println(item.getKey()+“”+item.getValue());
}
我终于解决了这个问题。以下是我的解决方案:
#set($valToKey = {})
$!valToKey.put("aaa", ["bbb", "ccc"])
$!valToKey.get("aaa").add("ddd")
## fill valToKey map
#foreach($item in $tmp.entrySet())
#foreach($value in $item.value)
#if($valToKey.containsKey($value))
$!valToKey.get($value).add($item.key) ## use of dummy to prevent printing 'true'
#else
$!valToKey.put($value, [$item.key])
#end
#end
#end
## print valToKey map
#foreach($item in $valToKey.entrySet())
$item.key
## print all values according to key
#foreach($value in $item.value)
$indent -> $value
#end
#end
您使用了什么虚拟工具阻止true打印?@user2900150对不起,该评论应该被删除:-)