使用java查找文本文件中单词的频率

发布于 2024-08-13 06:44:02 字数 80 浏览 2 评论 0原文

我已成功解析给定输入文本文件的全部内容并将每个单词存储在哈希集中。但现在我需要找到这个输入文件中每个单词的频率,关于我该如何做有什么建议吗? :)

Ive managed to parse the entire contents of a given input text file and store each word in a hash set. But now i need to find the frequenct of each of these words in this input file, any suggestions as to how I can go about? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2024-08-20 06:44:02

使用 HashMap 而不是 HashSet 并将此类作为值:

class Counter {
    public int frequency;
}

addWord() 则如下所示:

public void addWord (String word) {
    Counter c = map.get (word);
    if (c == null) {
        c = new Counter ();
        map.put(word, c);
    }
    c.frequency ++;
}

Use a HashMap instead of a HashSet and this class as the value:

class Counter {
    public int frequency;
}

addWord() then looks like this:

public void addWord (String word) {
    Counter c = map.get (word);
    if (c == null) {
        c = new Counter ();
        map.put(word, c);
    }
    c.frequency ++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文