gpt4 book ai didi

java - 无法在正确的泛型类型中使用正确的值

转载 作者:行者123 更新时间:2023-12-01 10:57:24 25 4
gpt4 key购买 nike

我有一个 HashMap,它不允许我在 hashmap 上使用 getOrDefault 方法。这是我的代码:

 public static HashMap<String, Integer> getInputWords(String location) {
HashMap<String, Integer> wordCount = new HashMap<String, Integer>();
try {
FileReader fReader = new FileReader(location);
BufferedReader bFReader = new BufferedReader(fReader);
String line = bFReader.readLine();
while(line != null) {
String[] strs = line.split(" ");
for(String tmp : strs) {
wordCount.put(tmp.toLowerCase(), getOrDefault(tmp.toLowerCase(), Integer.valueOf(0)) + 1); //error on getOrDefault method.
}
line = bFReader.readLine();
}
bFReader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return wordCount;
}

该方法给我一个错误:

The method getOrDefault(String, Integer) is undefined for the type Main07

(Main07是我的类(class))。

最佳答案

getOrDefault() 是一个 HashMap 方法,因此请在 HashMap 上调用它。

wordCount.getOrDefault(tmp.toLowerCase(), Integer.valueOf(0))

关于java - 无法在正确的泛型类型中使用正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33559375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com