【问题标题】:Create word count of text using hashmap使用 hashmap 创建文本的字数
【发布时间】:2014-01-17 22:15:19
【问题描述】:

我正在尝试为自己创建一个程序作为哈希图的教程。我要求用户输入文本并尝试将其拆分为哈希图,然后如果单词重复则增加计数。这是我的程序:

import java.util.*;
import java.lang.*;
import javax.swing.JOptionPane;
import java.io.*;
public class TestingTables
{
   public static void main(String args[])
   {
      {
      String s = JOptionPane.showInputDialog("Enter any text.");
      String[] splitted = s.split(" ");
      HashMap hm = new HashMap();
      int x;

      for (int i=0; i<splitted.length ; i++) {
         hm.put(splitted[i], i);
         System.out.println(splitted[i] + " " + i);
         if (hm.containsKey(splitted[i])) {
             x = ((Integer)hm.get(splitted[i])).intValue();
             hm.put(splitted[i], new Integer(x+1)); }
         }
      }
   }
}

当我输入“random random random”时,我得到: 随机 0 随机 1 随机 2

我需要改变什么才能得到: 随机 3 另外,我需要使用迭代器来打印出 hashmap,还是我使用的可以?

【问题讨论】:

  • 你为什么打电话给hm.put(splitted[i], i);
  • @MiserableVariable 我在 Java API 上找到了语法,这是他们在示例中使用的变量。我只是忘记改了。

标签: java for-loop count hashmap


【解决方案1】:

这应该可行,它是一个非常简单的实现..

        Map<String, Integer> hm = new HashMap<String, Integer>();
    int x;

    for (int i = 0; i < splitted.length; i++) {

        if (hm.containsKey(splitted[i])) {
            x = hm.get(splitted[i]);
            hm.put(splitted[i], x + 1);
        } else {
            hm.put(splitted[i], 1);
        }
    }

    for (String key : hm.keySet()) {

        System.out.println(key + " " + hm.get(key));
    }

【讨论】:

    【解决方案2】:
    import java.util.*;
    import java.lang.*;
    import javax.swing.JOptionPane;
    import java.io.*;
    public class TestingTables
    {
       public static void main(String args[])
       {
          {
          String s = JOptionPane.showInputDialog("Enter any text.");
          String[] splitted = s.split(" ");
          Map<String, Integer> hm = new HashMap<String, Integer>();
          int x;
    
          for (int i=0; i<splitted.length ; i++) {
             if (hm.containsKey(splitter[i])) {
                int cont = hm.get(splitter[i]);
                hm.put(splitter[i], cont + 1)
             } else {
                hm.put(splitted[i], 1);
             }
          }
       }
    }
    

    您的 Map 声明错误,请记住实现 Map 的正确方法。

    【讨论】:

    • 他的地图声明没有错,只是没有使用泛型(不推荐但也没有错)。
    • @peter.petrov 哦,好吧,我以前从未见过这样的声明。
    • @Vannens 我相信这是 java 5 版本提出的。
    【解决方案3】:

    您的初始化错误hm.put(splitted[i], i)。 您应该初始化为 0 或 1(计数,而不是索引)。

    所以先做这个循环。

            for (int i = 0; i < splitted.length; i++) {
                if (!hm.containsKey(splitted[i])) {
                    hm.put(splitted[i], 1);
                } else {
                    hm.put(splitted[i], (Integer) hm.get(splitted[i]) + 1);
                }
            }
    

    然后再做一个循环(遍历 HashMap 的键)并打印出计数。

            for (Object word : hm.keySet()){
                System.out.println(word + " " + (Integer) hm.get(word));
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-03
      • 2023-03-29
      • 1970-01-01
      • 2020-10-01
      • 2014-04-16
      • 2015-09-26
      • 2020-03-23
      • 2021-10-27
      相关资源
      最近更新 更多