Hashmap中的值乘以15?

时间:2016-04-26 22:12:29

标签: java hashmap key-value

所以我正在为CS课程编写一个涉及使用哈希图的作业,我需要跟踪一个单词出现在页面上的次数。

现在出于一些奇怪的原因,我遇到了最奇怪的错误,一切似乎都正常工作,一切都存储在地图中,但是,当我打印出来测试时,地图中的每个值都乘以15

成员只出现8次,但我的输出是120。

redding只显示32次,但我的输出是480。

以下是相关代码:

Map<String, Integer> found = new HashMap<>();

while (match.find()) {
    String word = match.group().toLowerCase();
    Integer check = found.get(word);

    if (check == null) {
        found.put(word, 1);
    } else {
        found.put(word, check+1);
    }
}
...
for (Map.Entry<String, Integer> entry : found.entrySet()) {
    String k = entry.getKey();
    Integer i = entry.getValue();

    System.out.println("Word: " + k + " \t\t\tFrequency: " + i);

}

有没有人知道这里会发生什么?

编辑:正则表达式的代码等:

String word_pattern = "[A-Za-z]{5,}";
String content = WebDoc.getBodyContent(url);

Matcher match = Pattern.compile(word_pattern).matcher(content);

如果问题在这里而不是我不明白为什么,因为这与我的教师的示例代码相符,并且他的示例运行没有这个问题。

1 个答案:

答案 0 :(得分:0)

当使用match.group()时,你不会只抓取你想要的那个匹配的单个实例。

考虑在第一个循环中输出check的值,看看那里发生了什么。