没有这样的方法错误:ImmutableList.copyOf()

fis*_*rds 31 java guava

我正在使用Guava-05-snapshot,使用Sun的JDK 1.6代码会破坏执行此代码段:

List<String> badpasswords = Lists.newArrayList( Password.badWords);
Collections.sort(badpasswords);
ImmutableList<String> tmp = ImmutableList.copyOf(badpasswords);
Run Code Online (Sandbox Code Playgroud)

特别是在ImmutableList.copyOf()调用上.此代码使用旧的Google-Collections代码已经工作了几个月.

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;
Run Code Online (Sandbox Code Playgroud)

Password.badWords是一个ImmutableSet<String>和可写数组的创建和排序工作完美.但尝试将阵列转换为ImmutableList失败.

Kev*_*ion 45

Guava是Google Collections的完全兼容超集 - 我们没有以不兼容的方式更改任何内容.(这是通过对最新的番石榴罐运行整个Google Collections测试套件(这是广泛的)来测试的.)

我相信你有一个google-collect的副本 - *.jar仍然会进入你的类路径.显式,或者因为其他jar包含它而不重新打包它.你只需要找到并删除它.

在Google Collections中,有一种ImmutableList.copyOf(Iterable)方法,并且没有公共ImmutableList.copyOf(Collection)方法.哪个没问题,因为集合也是可迭代的.在Guava中,我们添加了Collection重载.这是完全兼容的,因为用于编译的所有源仍然可以,并且先前编译的任何源将仍然引用原始方法.

如果您针对Guava进行编译但是针对Google Collections运行,则会出现此问题.我相信这很可能发生了什么.

  • 问题是Netbeans在缓存中保留太多东西的副作用."清洁和构建"没有解决问题.我做了一个完整的清理,并在项目的每个罐子里积累.解决方案是退出Netbeans,转到〜/ netbeans/6.7/var/cache并执行rm -rf* (2认同)
  • "Guava是Google Collections的完全兼容超集" - 显然不再存在了?http://code.google.com/p/gdata-java-client/issues/detail?id=344#c2 (2认同)