获取HashMap中使用最多的键的有效方法-Java
我有一个HashMap,其中键是一个单词,值是该字符串在文本中出现的次数。现在我想把这个HashMap减少到只有15个最常用的单词(出现次数最多)。你有没有办法有效地完成这项工作?你可以使用a并删除最近使用最少的物品。我认为解决这个问题的一种方法是:获取HashMap中使用最多的键的有效方法-Java,java,hashmap,Java,Hashmap,我有一个HashMap,其中键是一个单词,值是该字符串在文本中出现的次数。现在我想把这个HashMap减少到只有15个最常用的单词(出现次数最多)。你有没有办法有效地完成这项工作?你可以使用a并删除最近使用最少的物品。我认为解决这个问题的一种方法是: 创建hashMap.entrySet().toArray(新条目[]{})的数组 使用数组对其排序。排序,创建自己的比较器,该比较器将仅在条目上进行比较。getValue()(将其转换为整数)。按降序排列,即最早/最高,最晚/最低 在排序数组上迭
- 创建
的数组hashMap.entrySet().toArray(新条目[]{})
- 使用
,创建自己的数组对其排序。排序
,该比较器将仅在比较器
(将其转换为整数)。按降序排列,即最早/最高,最晚/最低条目上进行比较。getValue()
- 在排序数组上迭代,并在达到第15个值时中断
- 使用数组而不是Pindatjuh建议的ArrayList可能更好
public class HashTest {
public static void main(String[] args) {
class hmComp implements Comparator<Map.Entry<String,Integer>> {
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue();
}
}
HashMap<String, Integer> hm = new HashMap<String, Integer>();
Random rand = new Random();
for (int i = 0; i < 26; i++) {
hm.put("Word" +i, rand.nextInt(100));
}
ArrayList list = new ArrayList( hm.entrySet() );
Collections.sort(list, new hmComp() );
for ( int i = 0 ; i < 15 ; i++ ) {
System.out.println( list.get(i) );
}
}
}
公共类HashTest{
公共静态void main(字符串[]args){
类hmComp实现了比较器{
公共整数比较(条目o1,
入口(氧气){
返回o2.getValue()-o1.getValue();
}
}
HashMap hm=新的HashMap();
Random rand=新的Random();
对于(int i=0;i<26;i++){
hm.put(“单词”+i,兰特·耐克斯汀(100));
}
ArrayList list=新的ArrayList(hm.entrySet());
Collections.sort(list,new hmComp());
对于(int i=0;i<15;i++){
System.out.println(list.get(i));
}
}
}
编辑反向排序顺序Map=newhashmap();
Map<String, Integer> map = new HashMap<String, Integer>();
// --- Put entries into map here ---
// Get a list of the entries in the map
List<Map.Entry<String, Integer>> list = new Vector<Map.Entry<String, Integer>>(map.entrySet());
// Sort the list using an annonymous inner class implementing Comparator for the compare method
java.util.Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){
public int compare(Map.Entry<String, Integer> entry, Map.Entry<String, Integer> entry1)
{
// Return 0 for a match, -1 for less than and +1 for more then
return (entry.getValue().equals(entry1.getValue()) ? 0 : (entry.getValue() > entry1.getValue() ? 1 : -1));
}
});
// Clear the map
map.clear();
// Copy back the entries now in order
for (Map.Entry<String, Integer> entry: list)
{
map.put(entry.getKey(), entry.getValue());
}
//---在这里把条目放到地图上---
//获取地图中条目的列表
List=新向量(map.entrySet());
//使用为compare方法实现Comparator的注释性内部类对列表进行排序
sort(list,newcomparator(){
公共int比较(Map.Entry,Map.Entry entry1)
{
//对于匹配项返回0,-1表示小于,+1表示大于
返回(entry.getValue().equals(entry1.getValue())?0:(entry.getValue()>entry1.getValue()?1:-1));
}
});
//清除地图
map.clear();
//现在按顺序复制回条目
用于(映射条目:列表)
{
map.put(entry.getKey(),entry.getValue());
}
使用地图的前15个条目。或者修改最后4行,以便在地图中仅放置15个条目何时要减少列表?定期?你有没有试着用谷歌搜索这个问题,或者你只是想看看我们?@Artic:SO的全部目的是“成为编程相关问题的谷歌”。所以像“谷歌是你的朋友”这样的回答在这里是不受欢迎的。如果你不能回答,那么就不要评论“Google it”。我也不认为SO的海豚会问被问了数百万次的问题(理论计划)。“最近使用最少的项目”
LinkedHashMap
在重新插入条目时不会改变元素顺序。这不起作用。如果重复在最后集中会怎么样?