【问题标题】:Count words from list and save them and their count in a Hashmap从列表中计算单词并将它们及其计数保存在 Hashmap 中
【发布时间】:2014-10-16 09:23:42
【问题描述】:

我有一个名为wordsList 实例和一个名为mapHashMap 实例。我在列表中有某些单词,并计算它们以保存在HashMap 上。 HashMap 具有计数作为值和单词作为键。

我不知道我在这里做错了什么。非常感谢任何帮助。

有更好的实现吗?

for (String aword : words) {
            map = new HashMap<String, Integer>();
            if (map.containsKey(aword)) {
                int count;
                try {
                    count = map.get(aword);
                    count++;
                } catch (NullPointerException npe) {
                    count = 0;
                }
                map.put(aword, count);
                System.out.println(aword+"SS"+count);
            } else {
                map.put(aword, 0);
                System.out.println(aword+"else"+0);
            }
        }

我的else 子句总是被输入。

【问题讨论】:

    标签: java hashmap word-count


    【解决方案1】:

    您正在每次迭代中创建一个新地图。您应该将地图初始化移到循环之前。

    【讨论】:

    • @Nabin 好吧,我不明白您为什么需要捕获 NullPointerException,因为您在从地图中获取计数之前检查是否存在。
    • 还有别的吗?这是完成这项工作的最佳代码吗?有效率吗?
    • @Nabin 看起来不错,除了您需要进行一项更改外,当找不到单词时,将计数初始化为 1,而不是 0。
    • 谢谢。一旦我可以运行代码,我就已经发现它非常感谢。
    【解决方案2】:

    是的,那是因为您为每个单词都实例化了一个新的 HashMap:

    for (String aword : words) {
        map = new HashMap<String, Integer>();    // <- here
    ...
    

    将其移出循环:

    map = new HashMap<String, Integer>();
    for (String aword : words) {
        if (map.containsKey(aword)) {
     ...
    

    【讨论】:

      【解决方案3】:

      在 for 循环外创建地图

      map = new HashMap<String, Integer>();
      
      for (String aword : words) {         
                  if (map.containsKey(aword)) {
                      int count;
                      try {
                          count = map.get(aword);
                          count++;
                      } catch (NullPointerException npe) {
                          count = 0;
                      }
                      map.put(aword, count);
                      System.out.println(aword+"SS"+count);
                  } else {
                      map.put(aword, 0);
                      System.out.println(aword+"else"+0);
                  }
              }
      

      【讨论】:

        【解决方案4】:

        而不是使用 containsKey 然后 get 然后 put 我这样做:

            map = new HashMap<String, Integer>(); //put it here instead than in the for loop
            for (String aword : words) {
                Integer tmp = stats.put(word, 1); //tmp is the previous value associated with key "word"
                if (tmp != null) {
                    stats.put(word, ++tmp);
                }
            }
        

        【讨论】:

        • @Nabin 我想这是一种更短、更有效的方法,因为您可以避免过多地迭代地图。
        【解决方案5】:

        在循环外创建地图并移除 try-catch。

            Map<String, Integer> map = new HashMap<>();
            for (String word : words) {
                int count = map.containsKey(word) ? map.get(word) : 0;
                map.put(word, count + 1);
            }
        

        【讨论】:

          猜你喜欢
          • 2021-02-03
          • 2020-11-25
          • 1970-01-01
          • 2021-10-24
          • 1970-01-01
          • 1970-01-01
          • 2020-10-12
          • 2016-07-29
          • 1970-01-01
          相关资源
          最近更新 更多