具有静态泛型的类型安全、泛型、空集合

发布于 2024-08-28 06:05:38 字数 663 浏览 4 评论 0原文

我尽可能返回空集合和 null。我使用 java.io 在两种方法之间切换。 util.Collections

return Collections.EMPTY_LIST;
return Collections.emptyList();

其中 emptyList() 应该是类型安全的。但我最近发现:

return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());

等等。

我在 Eclipse Package Explorer 中看到了这个方法:

<clinit> () : void

但我没有看到这是如何在源代码(1.5)中完成的。这种神奇的愚蠢行为是怎么发生的!!

编辑: 静态泛型类型是如何实现的?

I return empty collections vs. null whenever possible. I switch between two methods for doing so using java.util.Collections:

return Collections.EMPTY_LIST;
return Collections.emptyList();

where emptyList() is supposed to be type-safe. But I recently discovered:

return Collections.<ComplexObject> emptyList();
return Collections.<ComplexObject> singletonList(new ComplexObject());

etc.

I see this method in Eclipse Package Explorer:

<clinit> () : void

but I don't see how this is done in the source code (1.5). How is this magic tomfoolerie happening!!

EDIT:
How is the static Generic type accomplished?

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

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

发布评论

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

评论(5

煮酒 2024-09-04 06:05:38
return Collections.<ComplexObject> emptyList();

使用它可以消除 Eclipse 中关于非泛型集合的警告。

话虽如此,由于空列表是不可变的并且 Java 在编译时擦除泛型类型,因此类型化空列表在功能上与非类型化空列表相同。

return Collections.<ComplexObject> emptyList();

Using this will get rid of warnings from Eclipse about non-generic collections.

Having said that, a typed empty list is going to be functionally identical to an untyped empty list due to empty list being immutable and Java erasing generic types at compile time.

柠北森屋 2024-09-04 06:05:38

编辑:静态泛型类型如何
完成了吗?

http://www.docjar.com/html/api/java /util/Collections.java.html

public class Collections {
    ...
    public static final List EMPTY_LIST = new EmptyList<Object>();
    ...
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    ...
}

如果您好奇,您可以查看 EmptyList 类的实现链接,但对于您的问题,这并不重要。

EDIT: How is the static Generic type
accomplished?

http://www.docjar.com/html/api/java/util/Collections.java.html

public class Collections {
    ...
    public static final List EMPTY_LIST = new EmptyList<Object>();
    ...
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    ...
}

You can see the link for the implementation of the EmptyList class if you're curious, but for your question, it doesn't matter.

雄赳赳气昂昂 2024-09-04 06:05:38

是静态初始化块。它是只执行一次的代码块(当加载类时)。

因此,

class  A  {
   static int x = 5;
}

可以不用写 One 来写:

class A {
   static int x;

   static {  // static initializer starts
      x = 5; 
   }
}

这两个类是等价的。在静态初始化块内,可以放置任意代码,从而使用复杂计算的结果初始化静态字段。

<clinit> is the static initializer block. It is a block of code which is executed exactly once (when the class is loaded).

So, instead of writing

class  A  {
   static int x = 5;
}

One can write:

class A {
   static int x;

   static {  // static initializer starts
      x = 5; 
   }
}

These two classes are equivalent. Inside a static initializer block one can place arbitrary code and thus initialize static fields with the results of complicated calculations.

梦幻的味道 2024-09-04 06:05:38

是编译期间收集类初始化代码的方法的名称。 (也就是说,static {} 块内的所有代码以及静态成员的初始值设定项,按源代码顺序排列。)

它与方法调用中的显式类型参数无关。

<clinit> is the name of the method into which the class initialization code is collected by during compilation. (That is, all of the code inside static {} blocks, and the initializers of static members, in source code order.)

It has nothing to do with explicit type parameters in method invocations.

暖阳 2024-09-04 06:05:38

对于那些正在寻找类似解决方案的人,但对于当前时间(2023 年 4 月),人们已经开始使用 List.of() 作为 Collections.emptyList 的简洁替代品()...

以同样的方式,类型作为

Collections.emptyList();

中方法的前缀使用相同的方法前缀模式:

List .of();

For those who are looking for a similar solution, but for more current time (2023/Apr) where one has moved on to using List.of() as a terse replacement for Collections.emptyList()...

In the same way the type is prefixed to the method in

Collections.<ComplexObject>emptyList();

Use the same method-prefix pattern:

List.<ComplexObject>of();

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