【发布时间】:2021-06-05 16:02:42
【问题描述】:
所以我在网上看到了一些代码,我在我的程序中实现了根据整数的值对 hashmap
如果有人可以为我指出正确的方向,这将是有帮助的,我什至不知道该用谷歌搜索什么来找出如何做到这一点,谢谢。
public List<String> getMaxList(int n, HashMap<String, Integer> itemCount){
List<Map.Entry<String, Integer>> maxList = new ArrayList<>();
Object[] a = itemCount.entrySet().toArray();
int n_iterator = n-1;
Arrays.sort(a, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Map.Entry<String, Integer>) o2).getValue()
.compareTo(((Map.Entry<String, Integer>) o1).getValue());
}
});
for (Object e : a) {
System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
+ ((Map.Entry<String, Integer>) e).getValue());
if (n_iterator <= 0){
break;
} else {
n_iterator--;
}
}
return null;
}
}
【问题讨论】:
标签: java performance dictionary hashmap comparator