How to count the number of occurrences of each word?

前端 未结 6 1509
梦如初夏
梦如初夏 2020-12-21 05:32

If I have an article in English, or a novel in English, and I want to count how many times each words appears, what is the fastest algorithm written in Java?

Some pe

6条回答
  •  感情败类
    2020-12-21 06:01

        Map countByWords = new HashMap();
        Scanner s = new Scanner(new File("your_file_path"));
        while (s.hasNext()) {
            String next = s.next();
            Integer count = countByWords.get(next);
            if (count != null) {
                countByWords.put(next, count + 1);
            } else {
                countByWords.put(next, 1);
            }
        }
        s.close();
    

    this count "I'm" as only one word

提交回复
热议问题