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

2024-04-22

我正在使用 Guava-05-snapshot 和 Sun 的 JDK 1.6 执行此代码片段时,代码会崩溃:

List<String> badpasswords = Lists.newArrayList( Password.badWords);
Collections.sort(badpasswords);
ImmutableList<String> tmp = ImmutableList.copyOf(badpasswords);

特别是在 ImmutableList.copyOf() 调用上。 该代码使用旧的 Google-Collections 代码已经运行了几个月。

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableList;

The Password.badWords is an ImmutableSet<String>可写数组的创建和排序工作完美。但尝试将数组转换为ImmutableList fail.


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

我相信您的类路径中仍然有 google-collect-*.jar 的副本。要么是明确的,要么是因为其他一些 jar 包含它而没有重新打包它。您只需找到它并将其删除即可。

在 Google Collection 中,有一个ImmutableList.copyOf(Iterable)方法,并且没有公开ImmutableList.copyOf(Collection)方法。这很好,因为集合也是可迭代的。在 Guava 中,我们添加了 Collection 重载。这是完全兼容的,因为用于编译的所有源代码仍然可以,并且之前编译的任何源代码仍然会引用原始方法。

如果您针对 Guava 进行编译,然后针对 Google Collections 运行,就会出现问题。我相信这很可能就是正在发生的事情。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

没有这样的方法错误:ImmutableList.copyOf() 的相关文章

随机推荐