将词频添加到哈希表

Add words frequency to Hashtable

我正在尝试编写一个程序,从文件中获取单词并将它们放入哈希表中。然后我必须像这样计算单词的频率和输出:单词,出现次数。 我知道我的添加方法搞砸了,但我不知道该怎么做。我是 java.

的新手
public class Hash {

private Hashtable<String, Integer> table = new Hashtable<String, Integer>();

public void readFile() {

    File file = new File("file.txt");

    try {

        Scanner sc = new Scanner(file);

        String words;

        while (sc.hasNext()) {
            words = sc.next();
            words = words.toLowerCase();

            if (words.length() >= 2) {
                table.put(words, 1);
                add(words);
            }
        }
        sc.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public void add(String words) {

    Set<String> keys = table.keySet();
    for (String count : keys) {
        if (table.containsKey(count)) {
            table.put(count, table.get(count) + 1);
        } else {
            table.put(count, 1);
        }
    }
}

public void show() {

    for (Entry<String, Integer> entry : table.entrySet()) {
        System.out.println(entry.getKey() + "\t" + entry.getValue());
    }
}

public static void main(String args[]) {

    Hash abc = new Hash();

    abc.readFile();

    abc.show();
}
}

这是我的file.txt

one one
two
three
two

输出:

two , 2
one , 5
three , 3
Set<String> keys = table.keySet();
for (String count : keys) {
    if (table.containsKey(count)) {
        table.put(count, table.get(count) + 1);
    } else {
        table.put(count, 1);
    }
}

现在,您正在增加地图中已有的键。相反,我不认为你想遍历任何东西,你只想让 words 的增量 if 条件,我认为它实际上只代表一个词。

if (table.containsKey(words)) {
   table.put(words, table.get(words) + 1);
} else {
   table.put(words, 1);
}

您可以删除添加功能。在将值设置为 1 之后,您尝试递增,而我会写

try (Scanner sc = new Scanner(file)) {
    while (sc.hasNext()) {
        String word = sc.next().toLowerCase();

        if (words.length() >= 2) {
            Integer count = table.get(word);
            table.put(word, count == null ? 1 : (count+1));
        }
    }
}

注意:在 Java8 中,您可以在一行中完成所有这些操作,并行处理每一行。

Map<String, Long> wordCount = Files.lines(path).parallel() 
                    .flatMap(line -> Arrays.asList(line.split("\b")).stream()) 
                    .collect(groupingByConcurrent(w -> w, counting()));

注意

map.merge(word, 1, (c, inc) -> c + inc);

或者

map.compute(word, c -> c != null ? c + 1 : 1);

版本比

更短并且可能更高效
if (table.containsKey(words)) {
   table.put(words, table.get(words) + 1);
} else {
   table.put(words, 1);
}

Integer count = table.get(word);
table.put(word, count == null ? 1 : (count+1));

此主题中的人建议。