对于HashMap类型,方法replace(String,ArrayList <>)是未定义的

And*_*ndi 1 java hashmap java-7

我的代码在我的机器上运行正常:

ArrayList<Posting> postings = invertedLists.get(key);
//---- Apply some changes on postings -----     
invertedLists.replace(key, postings);
Run Code Online (Sandbox Code Playgroud)

当我的朋友克隆代码时,它给了她这个错误:

The method replace(String, ArrayList<Posting>) is undefined for the type HashMap<String,ArrayList<Posting>>
Run Code Online (Sandbox Code Playgroud)

注意:invertLists是一个 HashMap<String,ArrayList<Posting>>

我们检查了我的项目; 我使用的是JavaSE-1.6,她使用的是JavaSE-1.7.

什么可能导致这个问题?

ysh*_*vit 6

Map.replace首先在Java 1.8中声明(参见底部的"since:1.8").您必须针对1.8 JDK进行编译(即使您在1.6模式下进行编译).可以在IDE中执行此操作,例如:将语言兼容性模式设置为一个版本,但针对另一个版本的JDK进行编译.我偶然做过几次.

你的朋友正在编译"普通"1.7,它没有声明该方法.