【问题标题】:Efficient way to get the most used keys in a HashMap - Java获取 HashMap 中最常用键的有效方法 - Java
【发布时间】:2010-03-13 17:42:45
【问题描述】:

我有一个 HashMap,其中键是一个单词,值是该字符串在文本中出现的次数。现在我想将此 HashMap 减少到只有 15 个最常用的单词(出现次数最多)。你有什么想法可以有效地做到这一点吗?

【问题讨论】:

  • 你想什么时候减少列表?定期?
  • 你是用谷歌搜索过这个问题还是只是想看看我们?
  • @Artic:SO 的重点是“成为编程相关问题的谷歌”。所以像“Google 是你的朋友”这样的回答在这里受欢迎。如果您无法回答,请不要评论“Google it”。
  • 而且我不认为 SO 的海豚会问被问过百万次的问题(理论计划)。

标签: java hashmap


【解决方案1】:

使用数组而不是 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) );
            }

        }
    }

编辑颠倒的排序顺序

【讨论】:

    【解决方案2】:

    我想解决这个问题的一种方法是:

    • 创建一个hashMap.entrySet().toArray(new Entry[]{}) 的数组。
    • 使用Arrays.sort 对其进行排序,创建您自己的Comparator,该Comparator 将仅在Entry.getValue() 上进行比较(将其转换为整数)。使其按降序排列,即最高/最高在前,较少/最低最新。
    • 遍历已排序的数组并在达到第 15 个值时中断。

    【讨论】:

      【解决方案3】:
      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());
          }
      

      使用地图的前 15 个条目。或者修改最后 4 行以仅将 15 个条目放入地图

      【讨论】:

        【解决方案4】:

        您可以使用LinkedHashMap 并删除最近最少使用的项目。

        【讨论】:

        • “最近最少使用的项目”,LinkedHashMap 在重新插入条目时不会更改元素顺序。这行不通。
        • 如果重复出现在最后会怎样?
        猜你喜欢
        • 2016-03-09
        • 2014-02-22
        • 2016-11-30
        • 2019-05-20
        • 2013-08-23
        • 2017-01-29
        • 2016-03-08
        • 2012-08-07
        • 2015-11-17
        相关资源
        最近更新 更多