构造 ImmutableSortedSet 而不发出警告

发布于 2024-10-06 17:19:11 字数 593 浏览 5 评论 0原文

我想要构造ImmutableSortedSet。我编写了如下代码 smt:

Set<String> obj = new HashSet<String>();
Comparator<String> myComparator = new Comparator<String>(){
    @Override
    public int compare(String o1, String o2) {
        return 0;
    }           
};
Set<String> ordered = ImmutableSortedSet.copyOf(obj)
    .orderedBy(myComparator).build();

但它会生成警告:

静态方法 orderBy(比较器) 来自 ImmutableSortedSet 类型应该 以静态方式访问

如何在没有 @SuppressWarnings("static-access") 的情况下删除此警告?谢谢。

I want construct ImmutableSortedSet. I wrote code smt like:

Set<String> obj = new HashSet<String>();
Comparator<String> myComparator = new Comparator<String>(){
    @Override
    public int compare(String o1, String o2) {
        return 0;
    }           
};
Set<String> ordered = ImmutableSortedSet.copyOf(obj)
    .orderedBy(myComparator).build();

but it generates warning:

The static method
orderedBy(Comparator) from the
type ImmutableSortedSet should
be accessed in a static way

How can I remove this warning without @SuppressWarnings("static-access")? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

無處可尋 2024-10-13 17:19:11

它向您发出该警告是因为 orderedBy 是静态方法,并且您在 ImmutableSortedSet 的实例上调用它。这通常意味着你认为自己在做一件事,而实际上你在做另一件事,这里就是这种情况。

结果是这段代码不会做你认为它会做的事情......它将丢弃由 copyOf(obj) 创建的 ImmutableSortedSet (它只是用于访问静态方法 orderedBy,可以直接使用)并返回一个空集,就像您刚刚调用 ImmutableSortedSet.orderedBy(myComparator).build()

这就是你想要做的(正如 R. Bemrose 所说):

ImmutableSortedSet<String> ordered = ImmutableSortedSet.copyOf(myComparator, obj);

为了后代,这是我最初匆忙发布的内容(具有相同的结果):

ImmutableSortedSet<String> ordered = ImmutableSortedSet.orderedBy(myComparator)
    .addAll(obj).build();

It's giving you that warning because orderedBy is a static method and you're calling it on an instance of ImmutableSortedSet. This often means you think you're doing one thing when really you're doing something else, and that's the case here.

The result is that this code isn't going to do what you think it does... it's going to throw away the ImmutableSortedSet created by copyOf(obj) (it's only being used to access the static method orderedBy, which could be used directly) and return an empty set, as if you had just called ImmutableSortedSet.orderedBy(myComparator).build().

Here's what you want to do (as R. Bemrose said):

ImmutableSortedSet<String> ordered = ImmutableSortedSet.copyOf(myComparator, obj);

For posterity, here's what I hastily posted initially (which has the same result):

ImmutableSortedSet<String> ordered = ImmutableSortedSet.orderedBy(myComparator)
    .addAll(obj).build();
黄昏下泛黄的笔记 2024-10-13 17:19:11

查看 Guava ImmutableSortedSet 文档后,您似乎实际上想要 copyOf 的其他重载之一。

具体来说,您需要 copyOf(Comparator, Collection) 重载:

Set<String> ordered = ImmutableSortedSet.copyOf(myComparator, obj);

After looking at the Guava ImmutableSortedSet docs, it appears that you actually want one of the other overloads to copyOf.

Specifically, you want the copyOf(Comparator, Collection) overload:

Set<String> ordered = ImmutableSortedSet.copyOf(myComparator, obj);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文