【发布时间】:2013-03-03 09:11:09
【问题描述】:
当找到或存在值时,我试图在哈希表中打印键值。此代码似乎不起作用。
Map<String,Integer> map = new HashMap<String, Integer>();
for(int j=0;j<al.size();j++){
Integer count = map.get(al.get(j));
map.put(al.get(j), count==null?1:count+1); //auto boxing and count
}
int max = Collections.max(map.values());
if( map.containsValue(max))
{
System.out.println(map.keySet());
}
【问题讨论】:
-
如果 hashmap 包含最大值,则应打印其对应的键。哈希表和地图非常新。帮助将不胜感激。谢谢
-
是打印所有的键而不是你想要的吗?
-
是的,它正在打印所有的键。