简体   繁体   中英

Cannot use correct value in correct generic type

I have a HashMap which doesn't allow me to use the getOrDefault method on the hashmap. Here is my code:

 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 is giving me an error of:

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

(Main07 is my class).

getOrDefault()HashMap方法,因此请在您的HashMap上调用它。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM