如何利用哈希表来保存单词和使用频率?

发布于 2024-12-12 06:19:06 字数 123 浏览 0 评论 0原文

我现在很困惑。我应该编写一个使用哈希表的程序。哈希表保存单词及其使用频率。 “Word”类包含一个计数器和字符串。如果该单词已在表中,则其频率会增加。我一直在研究如何做到这一点,但只是迷失了。我需要指明正确的方向。任何帮助都会很棒。

I am so confused right now. I am supposed to write a program that uses a hashtable. The hashtable holds words along with their frequency of use. The class "Word" holds a counter and the string. If the word is already in the table then its frequency increases. I have been researching how to do this but am just lost. I need to be pointed in the right direction. Any help would be great.

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

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

发布评论

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

评论(5

小清晰的声音 2024-12-19 06:19:07

这段代码应该可以解决你的问题

  Hashtable <String, Word> myWords = new Hashtable<String, Word>();

  Word w = new Word("test");
  Word w = new Word("anotherTest");

  String inputWord = "test";

  if (myWords.containsKey(inputWord)){
      myWords.get(inputWord).setCounter(myWords.get(inputWord).getCounter+1);
  }

This piece of code should solve your problem

  Hashtable <String, Word> myWords = new Hashtable<String, Word>();

  Word w = new Word("test");
  Word w = new Word("anotherTest");

  String inputWord = "test";

  if (myWords.containsKey(inputWord)){
      myWords.get(inputWord).setCounter(myWords.get(inputWord).getCounter+1);
  }
晚雾 2024-12-19 06:19:07

鉴于类 Word 有一个计数器和一个字符串,我将使用 HashMap。如果您的输入是 String 数组,您可以使用以下方法完成类似的操作:

public Map<String, Word> getWordCount(String[] input) {
    Map<String, Word> output = new HashMap<String, Word>();

    for (String s : input) {
        Word w = output.get(s);
        if (w == null) {
            w = new Word(s, 0);
        }
        w.incrementValue(); // Or w = new Word(s, w.getCount() + 1) if you have no such function
        output.put(s, w);
    }

    return output;

}

Given that the class Word has a counter and a string, I'd use a HashMap<String, Word>. If your input is an array of Strings, you can accomplish something like this by using:

public Map<String, Word> getWordCount(String[] input) {
    Map<String, Word> output = new HashMap<String, Word>();

    for (String s : input) {
        Word w = output.get(s);
        if (w == null) {
            w = new Word(s, 0);
        }
        w.incrementValue(); // Or w = new Word(s, w.getCount() + 1) if you have no such function
        output.put(s, w);
    }

    return output;

}

少女七分熟 2024-12-19 06:19:06
Hashtable<String, Word> words = new Hashtable<String, Word>();

public void addWord(String s) {
    if (words.containsKey(s) {
        words.get(s).plusOne();
    } else {
        words.put(s, new Word(s));
    }
}

这样就可以了。

Hashtable<String, Word> words = new Hashtable<String, Word>();

public void addWord(String s) {
    if (words.containsKey(s) {
        words.get(s).plusOne();
    } else {
        words.put(s, new Word(s));
    }
}

This will do it.

慢慢从新开始 2024-12-19 06:19:06

如今,对于任何新的 Java 代码来说,哈希表都是一个不寻常的选择。我认为这是某种练习。

我会对任何尚未更新以使用新机制的练习感到稍微担心。

在任何单线程场景中,HashMap 都会比 Hashtable 提供更好的性能。

但正如 Emmanuel Bourg 指出Bag 将为您完成所有这一切根本不需要 Word 类:只需将 String 对象添加到 Bag 中,bag 就会自动为您计数。

不管怎样,你被要求使用地图,而地图可以让你通过使用钥匙快速找到东西。键可以是任何对象,字符串非常常用:它们是不可变的,并且具有良好的 hashCode 和 equals 实现,这使它们成为理想的键。

Map 的 javadoc 讨论了如何使用地图。 Hashtable 是该接口的一种实现,尽管它不是一个特别好的实现。

您需要一个好的密钥来让您快速找到现有的 Word 对象,以便您可以增加计数器。虽然您可以将 Word 对象本身设置为键,但您需要做一些工作:更好的方法是使用 Word 包含的字符串作为键。

您可以通过查找以 String 作为键的值对象来确定 Word 是否已在映射中。

Hashtable would be an unusual choice for any new Java code these days. I assume this is some kind of exercise.

I would be slightly concerned by any exercise that hadn't been updated to use newer mechanisms.

HashMap will give you better performance than Hashtable in any single threaded scenario.

But as Emmanuel Bourg points out, Bag will do all of this for you without needing the Word class at all: just add String objects to the Bag, and the bag will automatically keep count for you.

Anyway, you're being asked to use a Map, and a map lets you find things quickly by using a key. The key can be any Object, and Strings are very commonly used: they are immutable and have good implementations of hashCode and equals, which make them ideal keys.

The javadoc for Map talks about how you use maps. Hashtable is one implementation of this interface, though it isn't a particularly good one.

You need a good key to let you find existing Word objects quickly, so that you can increment the counter. While you could make the Word object itself into the key, you would have some work to do: better is to use the String that the Word contains as the key.

You find whether the Word is already in the map by looking for the value object that has the String as its key.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文