我有一个类型的HashMap:
HashMap<String, Integer> h = new HashMap<String, Integer>();
Run Code Online (Sandbox Code Playgroud)
HashMap包含一个字符串列表,而Integer是一个计数器,用于查找String的次数.我希望能够做的是根据整数对HashMap进行排序,然后按字符串的字母顺序排序.
目前我正在记录一个单词的最大出现(变量名为max)并显示如下值:
public void print(){
while(max > 0){
for (String key : h.keySet()){
if(h.get(key) == max){
System.out.println(key + " " + h.get(key));
}
}
max--;
}
}
Run Code Online (Sandbox Code Playgroud)
其中不按字母顺序对值进行排序,也访问HashMap最大*h(大小)次.
什么是更好的解决方案?