我有一个在我的机器上运行良好的代码:
ArrayList<Posting> postings = invertedLists.get(key);
//---- Apply some changes on postings -----
invertedLists.replace(key, postings);
当我的 friend 克隆代码时,它给了她这个错误:
The method replace(String, ArrayList<Posting>) is undefined for the type HashMap<String,ArrayList<Posting>>
注意:invertedLists 是一个 HashMap<String,ArrayList<Posting>>
我们检查了我的项目;我使用的是 JavaSE-1.6,她使用的是 JavaSE-1.7。
什么会导致这个问题?
最佳答案
Map.replace
首次在 Java 1.8 中声明(参见底部的“since: 1.8”)。您必须针对 1.8 JDK 进行编译(即使您是在 1.6 模式下进行编译)。可以在 IDE 中执行此操作,例如:将语言兼容模式设置为一个版本,但针对另一个版本的 JDK 进行编译。我已经不小心这样做了几次。
您的 friend 正在“普通”1.7 上编译,它没有声明该方法。
关于java - 方法 replace(String, ArrayList<>) 未定义 HashMap 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24290452/