可能重复:
How to sort a Map<Key, Value> on the values in Java?根据Value对Key进行排序HashMap?
我有型的一个HashMap:
HashMap<String, Integer> h = new HashMap<String, Integer>();
HashMap中包含字符串和整数的列表是一个计数器发现字符串的次数。我希望能够做的是基于Integers对HashMap进行排序,然后按字符串的字母顺序排序。
在我保持一个字(变量命名为最大值)的最大发生的记录的时刻,显示值如下:
public void print(){
while(max > 0){
for (String key : h.keySet()){
if(h.get(key) == max){
System.out.println(key + " " + h.get(key));
}
}
max--;
}
}
这并不值按字母顺序排序,也它访问HashMap max * h(size)倍。
什么是更好的解决方案?
@krock很好找。是的,完全相同的问题。 – cletus 2010-06-19 03:14:13