【问题标题】:Printing Individual Words Without Repeating?打印单个单词而不重复?
【发布时间】:2019-04-20 16:36:37
【问题描述】:

我想列出文本文件中的每个唯一单词以及在其中找到每个单词的次数。 我尝试使用if 循环,但我不确定如何在计算后消除已列出的单词。

for (int i = 0; i < words.size(); i++) {
    count = 1;
    //Count each word in the file and store it in variable count 
    for (int j = i + 1; j < words.size(); j++) {
        if (words.get(i).equals(words.get(j))) {
            count++;
        }

    }
    System.out.println("The word " + words.get(i) + " can be 
    found " + count + " times in the file.");
}

文本文件的内容是“Hello world.Hello world.”,程序将打印以下内容: The word Hello can be found 2 times in the file. The word world can be found 2 times in the file. The word Hello can be found 1 times in the file. The word world can be found 1 times in the file.

【问题讨论】:

标签: java counter


【解决方案1】:

我建议利用 HashMap 来解决这个问题。简单来说,HashMap就是一个key-value对,对key进行hash,搜索复杂度为O(1)。

只对单词列表进行一次迭代,并继续将遇到的单词存储在 HashMap 中。当你遇到一个单词时,检查它是否已经存在于 HashMap 中。如果不存在,则将其添加到地图中,键为单词本身,值为 1。 如果存在单词alrady,则将值加1。

完成迭代后,HashMap 将包含唯一单词的键值对与它们的计数!

以防万一您不了解 java 中的地图 - https://www.javatpoint.com/java-hashmap

【讨论】:

    【解决方案2】:

    您需要使用ArrayList 来存储已经找到的单词,然后,您需要检查文件中的每个单词,无论它是否存在于 ArrayList 中。如果该单词存在于 ArrayList 中,则需要忽略该单词。否则,将该单词添加到 ArrayList。 给你的示例代码:

    ArrayList<String> found_words=new ArrayList<String>();
    public static void main(String arguments[])
    {
        String data=""; //data from your file
        String[] words=data.split("\\s"); //split the string into individual words
        for(int i=0;i<words.length;i++)
        {
            String current_word=words[i];
            if(!is_present(current_word))
            {
                found_words.add(current_word);
                int count=1;
                for(int j=i+1;j<words.length;j++)
                {
                    if(words[j].equals(words[i]))
                        ++count;
                }
                System.out.println("The word "+current_word+" can be found "+count+" times in the file.");
            }
        }
    }
    static boolean is_present(String word)
    {
        for(int i=0;i<found_words.size();i++)
        {
            if(found_words.get(i).equals(word))
                return true;
        }
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      public void printWordOccurence(String filePath) throws FileNotFoundException {
              if (filePath.isEmpty())
                  return;
      
              File file = new File(filePath);
              Scanner input = new Scanner(file);
              HashMap<String, Integer> wordOccurence = new HashMap<>();
      
      
              while (input.hasNext()) {
                  wordOccurence.merge(input.next(), 1, Integer::sum);
              }
      
              for (String word : wordOccurence.keySet()) {
                  System.out.println(word + " appears " + wordOccurence.get(word) + " times");
              }
          }
      

      【讨论】:

      • 可以使用 Java 8 中引入的 Map 接口的 merge 方法,而不是在 while 循环中编写 if-else。
      • 现在您可以使用Integer::sum 方法引用而不是(val1, val2) -&gt; val1 + val2 :)
      猜你喜欢
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多