【问题标题】:Iterating HashMap with string key [duplicate]使用字符串键迭代 HashMap [重复]
【发布时间】:2018-03-08 05:19:02
【问题描述】:

有没有办法遍历java中的hashmap,其中键是字符串,值是整数,试图找到出现频率最高的单词

【问题讨论】:

    标签: java hashmap


    【解决方案1】:

    这里是解决方案。 “试图找到最常出现的词”部分我不清楚

    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }
    

    【讨论】:

      【解决方案2】:

      不明白“试图找到出现频率最高的词”。你可以试试这个。

      Map<String, Integer> map = new HashMap<String, Integer>();
          map.put("k1", 2);
          map.put("k3", 3);
          map.put("k2", 4);
          Set keySet = map.keySet();
          Iterator it = keySet.iterator();
          while (it.hasNext()) {
              String key = (String) it.next();
              System.out.println(map.get(key));
          }
      

      【讨论】:

      • 如果可以在 Set 上运行 for 循环,为什么要使用迭代器
      猜你喜欢
      • 2013-11-17
      • 1970-01-01
      • 2019-01-17
      • 2014-04-04
      • 2013-07-06
      • 2011-03-21
      • 2010-12-02
      • 2010-12-03
      • 2020-07-23
      相关资源
      最近更新 更多